<?php


require_once('config.php');





if(($response["error"] == true))
{
    echo(json_encode($response));
}

if(($response["error"] == false))
{
    if((isset($_POST["request"])) && ($_POST["request"] == "get_categories"))
    {
        $records        =   array();
        $sql            =   "   SELECT M.category_id CATEGORY_ID, D.category_name CATEGORY_NAME, 
                                REPLACE(LOWER(D.category_name), ' ', '-') CATEGORY_KEYWORD, COUNT(M.id) PRODUCTS 
                                FROM product_mst M, category D  
                                WHERE M.category_id = D.id
                                GROUP BY M.category_id, D.category_name";
        $stmt = $db->prepare($sql); 
        $stmt->execute();
        $result     =   $stmt->get_result();
        while($row = mysqli_fetch_assoc($result))
        {
            $records[]   =   $row;
        }
        $html   =   '';
        foreach($records as $k => $v)
        {
            $html  .=   '<div class="row">';
            $html  .=   '<div class="col-lg-12">';
            $html  .=   '<div class="form-check">';
            $html  .=   '<input class="form-check-input CI" type="checkbox" value="'.$v["CATEGORY_KEYWORD"].'" id="categoryinput_'.$v["CATEGORY_ID"].'" name="categoryinput">';
            $html  .=   '<label class="form-check-label" for="categoryinput_'.$v["CATEGORY_ID"].'">'.$v["CATEGORY_NAME"].' ('.$v["PRODUCTS"].')</label>';
            $html  .=   '</div>';
            $html  .=   '</div>';
            $html  .=   '</div>';
        }

        $html  .=   '<div class="row">';
        $html  .=   '<div class="col-lg-12">';
        $html  .=   '<button id="applybtn" class="btn btn-dark" style="width: 100%; margin-top: 20px">Apply Filters</button>';
        $html  .=   '<button class="btn btn-outline-primary" style="width: 100%; margin-top: 10px">Clear Filters</button>';
        $html  .=   '</div>';
        $html  .=   '</div>';
        $response["html"]    =  $html;
        echo(json_encode($response));
    }

}












































?>