<?php
require_once 'config.php';
function arraymerger($c1,$c)
	{
	    $arr=array();
	    $i=0;$k=0;$l=0;		//echo count($c);echo count($c1); die;
	    if (count($c1)>(count($c)/2)){
	    for($j=0;$j<count($c1);$j++)
	    {
              if (!is_null($c[$i])){ $arr[$k]=$c[$i];
              $k++;$i++;
              if($i==count($c)){
                  $i=0;
                  $l=1;
              }}
                if (!is_null($c[$i])){ $arr[$k]=$c[$i];
              $k++;$i++;
              if($i==count($c)){
                  $i=0;
                  $l=1;
              }}
              if($i==count($c)){
                  $i=0;
                  $l=1;
              }
              $arr[$k]=$c1[$j];
              $k++;
	    }
	        
	    }
	    else
	    {	
	        for($j=0,$i=0,$k=0;$i<count($c);$i++) {    
	            $arr[$k]=$c[$i];  
	            $k++;           
	            $p=$i%2;
	            if (($p==1) || ($i==(count($c)-1))){   
	               if (!is_null($c1[$j])){
	                   $arr[$k]=$c1[$j];    
	                $k++;	 $j++;
	                if($j==count($c1)){                 
	                    $j=0;              
	                    $l=1; 
	                    }}}	    }}
/*	if ($l==0){
	    for(;$i<count($c);$i++){
	        $arr[$k]=$c[$i];
	        $k++;
	    }
	}*/
	return $arr;
	}
//$error=false;
$errors=array();
$cities=array();
$cities1=array();
$smsflag=1;
if(isset($_GET['smsflag'])){
$smsflag=$_GET['smsflag'];}
$query='SELECT * FROM imagebyfolder where leaderid='.$_GET['user_id'];

$row = mysqli_query($con,$query);
     
    if (mysqli_num_rows($row) >= 1) {
		//$r=mysqli_fetch_array($row)	;
			
		 while($r=mysqli_fetch_array($row)){
			 
			$cities[]=['id'=>$r['id'],'type'=>'banners','user_id'=>$r['leaderid'],'image'=>$r['image'],'folder_id'=>$r['folderid']];
			
			// $cities['images']=$images;
			
			
		}   
		//$errors['banners']=$cities;
		
    } else {
        
		
    }
	if ($smsflag==1){
	$query1='SELECT * FROM trendyads ';

$row1 = mysqli_query($con,$query1);
     
    if (mysqli_num_rows($row1) >= 1) {
		//$r=mysqli_fetch_array($row)	;
			
		 while($r1=mysqli_fetch_array($row1)){
			 
			$cities1[]=['id'=>$r1['id'],'type'=>'smsbanners','image'=>$r1['image'],'description'=>$r1['description'],'status'=>$r1['status'],'views'=>$r1['views']];
			
			// $cities['images']=$images;
			
			
		}   } }
		//$errors['smsbanners']=$cities1;
		//$errors['product_images']=$images;
		
		if ($smsflag==1){
			$city=arraymerger($cities1,$cities); }  else { $city=$cities;}
	$city1=$city;
	//	shuffle($city1);
		
		if ((count($city)==0)){
        
		$errors['success']='There is no banners found';
    } else {
		$errors['success']='Instagram banners data';
	//	shuffle($city1);
		$errors['allbanners']=$city1;   
	}
	 
	echo json_encode($errors);
	
?>