<?php

require("dbconnect.php");
header('Content-Type: text/html; charset=utf-8');

$maindb = "product";
$brand = '';
$price = '';
$category = '';
$catqry = '';

if (!empty($_REQUEST["category"])) {
    $catqry = 'AND a.`type`=' . $_REQUEST['category'];
}
if (!empty($_REQUEST["stype"])) {
    $stype = 'AND a.`subtype`=' . $_REQUEST['stype'];
}

// mysql_query("SET CHARACTER SET utf8"); 
$getProduct = mysqli_query($conn, "SELECT a.*, b.name as `subtname` FROM `$maindb` a, `subtype` b WHERE a.`code` !='' AND a.`subtype` = b.`code` $catqry $stype ORDER BY `code` DESC")
        or die(mysql_error($conn));

$prodArray = array();
while ($row = mysqli_fetch_assoc($getProduct)) {

    $getstk = mysqli_query($conn, "SELECT sum(qty) as `totqty` FROM `transacn` where `itemcode`='$row[code]'") or die(mysqli_error($conn));
    $fstkqty = mysqli_fetch_object($getstk);
    if ($fstkqty->totqty > 0) {
        $outofstock = "0";
    } else {
        $outofstock = "1";
    }

    $prodArray[] = ["code" => $row['code'],
        "dname" => $row['dname'],
        "largeimg" => $row['largeimg'],
        "smallimg" => $row['smallimg'],
        "cost" => $row['cost'],
        "cost2" => $row['cost2'],
        "outstkcode" => $outofstock];
}
header("Content-type: application\json");
$productJson = json_encode($prodArray);
echo $productJson;
?>