<?php 

$prod = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
    FROM product INNER JOIN shop_settings on 
    product.protime != '' and product.shop_id = shop_settings.shop_id
    where product.status='1' and product.popular = '1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
    and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
    or product.status='1' and product.popular = '1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
    and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time') order by product.id desc limit 10");

// $prod = $con->query("select * from product where TIME_TO_SEC(prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(prostoptime) < TIME_TO_SEC('$current_time') and status=1 and popular = 1 order by id desc limit 10");

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_related_image'] = $row['prel'];
    // $result['seller_name'] = $row['sname'];
    $result['short_desc'] = $row['psdesc'];
    $a = explode('$;',$row['pgms']);
    //print_r($a[0]);
    $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'];
    $d[] = $result;
}

$slist = $con->query("select * from home_section where status = '1' and shop_id = '$shop_id' and shop_code = '$shop_code'")->num_rows;
if($slist !=0)
{
    $plist = $con->query("SELECT home_section.*, shop_settings.start_time, shop_settings.end_time 
    FROM home_section INNER JOIN shop_settings on 
    home_section.cattime != '' and home_section.shop_id = shop_settings.shop_id
    where home_section.status='1' and home_section.shop_id = '$shop_id' and home_section.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (home_section.cattime = 'Default', shop_settings.start_time, home_section.catstarttime)) < TIME_TO_SEC('$current_time')
    and TIME_TO_SEC(IF (home_section.cattime = 'Default', shop_settings.end_time, home_section.catstoptime)) > TIME_TO_SEC('$current_time') 
    or home_section.status='1' and home_section.shop_id = '$shop_id' and home_section.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (home_section.cattime = 'Customise',home_section.catstarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
    and TIME_TO_SEC(IF (home_section.cattime = 'Customise',home_section.catstoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')");
    
    // $plist = $con->query("select * from home where TIME_TO_SEC(catstarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(catstoptime) < TIME_TO_SEC('$current_time') and status = 1");
    
    $sev = array();
    while($rp = $plist->fetch_assoc())
    {
        $rpq = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
        FROM product INNER JOIN shop_settings on 
        product.protime != '' and product.shop_id = shop_settings.shop_id
        where product.cid=".$rp['cid']." and product.sid=".$rp['sid']." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and 
        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
        or product.cid=".$rp['cid']." and product.sid=".$rp['sid']." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and 
        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time') Order by product.id DESC");
        
        // $rpq =  $con->query("select * from product where status=1 and sid=".$rp['sid']." and cid=".$rp['cid']."  order by id desc");
        $section = array();
        $sep = array();
        while($rps = $rpq->fetch_assoc())
        {
            $section['id'] = $rps['id'];
            $section['product_name'] = $rps['pname'];
            $section['product_image'] = $rps['pimg'];
            $section['product_type'] = $rps['ptype'];
        // 	$section['product_related_image'] = $rps['prel'];
        //     $section['seller_name'] = $rps['sname'];
            $section['short_desc'] = $rps['psdesc'];
            $a = explode('$;',$rps['pgms']);
            //print_r($a[0]);
            $ab = explode('$;',$rps['pprice']);
            $k=array();
            for($i=0;$i<count($a);$i++)
            {
                $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
            }
            
            $section['price'] = $k;
        	$section['discount'] = $rps['discount'];
            $section['stock'] = $rps['stock'];
            $sep[] = $section;
        }
        $sev['title'] = $rp['title'];
        $sev['product_data'] = $sep;
        $sec[] = $sev;
    }
}

?>