<?php
// Database connection settings
require_once 'shikanisha.kts.php';

// Get the search term from the AJAX request
$searchTerm = $_GET['search'];

// SQL query to search for ingredients
$sql = "SELECT * FROM ingredients WHERE name LIKE '%$searchTerm%'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Array to hold search results
    $ingredients = array();

    // Fetch data and add to array
    while ($row = $result->fetch_assoc()) {
        $ingredient = array(
            'id' => $row['id'],
            'name' => $row['name'],
            'unit' => $row['unit'],
            'cost_price' => $row['cost_per_unit'],
            // Add more fields as needed, e.g., 'unit' => $row['unit']
        );
        $ingredients[] = $ingredient;
    }

    // Return JSON response
    header('Content-Type: application/json');
    echo json_encode($ingredients);
} else {
    // No ingredients found
    echo json_encode(array());
}

// Close the connection
$conn->close();
?>
