<?php
// get_companies.php

header('Content-Type: application/json');

// Include your DB connection file
include 'includes/dbconn.php';

// Fetch the companies from the database
$query = "SELECT company_id, name FROM companies ORDER BY name ASC";
$result = $conn->query($query);

$companies = [];

while ($row = $result->fetch_assoc()) {
    $companies[] = [
        'id' => $row['company_id'], // id is needed for Select2 to recognize it properly
        'text' => $row['name'] // text is what is displayed in the dropdown
    ];
}

// Close the database connection if it's no longer needed
$conn->close();

// Wrap the array in an 'items' key
echo json_encode(['items' => $companies]);
?>
