<?php
// Include the database connection file
include("includes/db.php");

if (isset($_POST['query'])) {
    // Prepare the query with a placeholder
    $query = "SELECT teacher_id, name FROM teachers WHERE name LIKE ? LIMIT 5";
    
    // Prepare the statement
    $statement = $conn->prepare($query);
    
    // Create the search term with wildcards
    $search = '%' . $_POST['query'] . '%';
    
    // Bind the parameter (the search term)
    $statement->bind_param('s', $search);
    
    // Execute the statement
    $statement->execute();
    
    // Get the result
    $result = $statement->get_result();
    
    // Initialize the output
    $output = '<ul class="list-group">';
    
    // Check if any rows are returned
    if ($result->num_rows > 0) {
        // Loop through the results
        while ($row = $result->fetch_assoc()) {
            $output .= '<li class="list-group-item list-group-item-action teacher-name-item" data-id="' . $row['teacher_id'] . '">' . $row['name'] . '</li>';
        }
    } else {
        // If no teacher is found
        $output .= '<li class="list-group-item">No Teacher Found</li>';
    }
    
    // Close the list
    $output .= '</ul>';
    
    // Echo the output
    echo $output;
}
?>
