Mini Shell

Direktori : /home/haworld/public_html/admin/
Upload File :
Current File : //home/haworld/public_html/admin/get_subcategories.php

<?php
include('include/connection_admin.php');
if (isset($_GET['categoryId'])) {
    $categoryId = $_GET['categoryId'];

    // Prepare and execute SQL query to fetch subcategories based on the selected category
    $sql = "SELECT * FROM `subcategory` WHERE `cat_id` = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param("i", $categoryId);
    $stmt->execute();
    $result = $stmt->get_result();

    // Initialize an array to store subcategories
    $subcategories = array();

    // Fetch subcategories and store them in the array
    while ($row = $result->fetch_assoc()) {
        $subcategories[] = $row;
    }

    // Close prepared statement and database connection
    $stmt->close();
    $con->close();

    // Send the array of subcategories as JSON response
    header('Content-Type: application/json');
    echo json_encode($subcategories);
} else {
    // If categoryId is not provided, return an empty response or an error message
    echo json_encode(['error' => 'Category ID not provided']);
}
?>