<?php 

require 'db.php';

$cat = $con->query("select * from category");
while($rows = $cat->fetch_assoc())
{
    $p['id'] = $rows['id'];
	$p['catname'] = $rows['catname'];
	$p['catimg'] = $rows['catimg'];
	$p['count'] = $con->query("select * from subcategory where cat_id=".$rows['id']."")->num_rows;
	$p['catstatus'] = $rows['catstatus'];
	$category[] = $p;
}

$subcat = $con->query("select * from subcategory");
while($rows = $subcat->fetch_assoc())
{
    $s['id'] = $rows['id'];
	$s['subcatname'] = $rows['name'];
	$s['subcatimg'] = $rows['img'];
	$cat =  $con->query("select catname from category where id=".$rows['cat_id']."");
	if($row = $cat->fetch_assoc())
	$s['catname'] = $row['catname'];
	$s['catstatus'] = $rows['catstatus'];
	$subcategory[] = $s;
}

$prod = $con->query("select * from product");
while($row = $prod->fetch_assoc())
{
	$result['id'] = $row['id'];
    $result['product_name'] = $row['pname'];
    $result['product_image'] = $row['pimg'];
    $result['product_type'] = $row['ptype'];
    $result['product_desc'] = $row['psdesc'];
    $result['popular'] = $row['popular'];
    $result['status'] = $row['status'];
    
    $result['cattime'] = $row['protime'];
    if($result['cattime'] == "Default")
	{
	    $sel = $con->query("select start_time,end_time from setting where id='1'");
        if($rows = $sel->fetch_assoc())
        {
            $result['catstarttime'] = $rows['start_time'];
            $result['catstoptime'] = $rows['end_time'];
        }
	}
	else
	{
	    $result['catstarttime'] = $row['prostarttime'];
	    $result['catstoptime'] = $row['prostoptime'];
	}
    
    $cat =  $con->query("select catname from category where id=".$row['cid']."");
    if($row1 = $cat->fetch_assoc())
    $result['catname'] = $row1['catname'];
    
    $scat =  $con->query("select name from subcategory where id=".$row['sid']."");
    if($row2 = $scat->fetch_assoc())
    $result['subcatname'] = $row2['name'];
    
    $a = explode('$;',$row['pgms']);
    $ab = explode('$;',$row['pprice']);
    $k=array();
    for($i=0;$i<count($a);$i++)
    {
        $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
    }
    
    $result['price'] = $k;
    $result['discount'] = $row['discount'];
    $result['stock'] = $row['stock'];
    $product[] = $result;
    }

  $json_array = array(categorylist=>$category,subcategorylist=>$subcategory,productlist=>$product);
  echo json_encode($json_array); 
?>