Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise
<?php  
$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select * from slider');  
$row->execute();//execute the query  
$json_data=array();//create the array  
foreach($row as $rec)//foreach loop  
{  
    $json_array['id']=$rec['id'];  
    $json_array['image_url']=$rec['image_url'];  
    $json_array['link_image']=$rec['link_image'];  
    array_push($json_data,$json_array);  

}  
echo json_encode($json_data);  
$row1=$db->prepare('select * from collections');  
$row1->execute();//execute the query  
$json_data1=array();//create the array  
foreach($row1 as $rec1)//foreach loop  
{  
    $json_array1['cid']=$rec1['id'];  
    $json_array1['cimage_url']=$rec1['image_url'];  
    $json_array1['cheading']=$rec1['heading']; 
    $json_array1['csubheading']=$rec1['subheading'];  
    array_push($json_data1,$json_array1);  
}  
echo json_encode($json_data1);  
?>

and the output is coming like that :

[{"id":"1","image_url":"http:\/\/fvilla.in\/app\/slider\/first.jpg","link_image":""},{"id":"2","image_url":"http:\/\/fvilla.in\/app\/slider\/second.jpg","link_image":""},{"id":"3","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""}]

[{"cid":"1","cimage_url":"http:\/\/fvilla.in\/app\/collections\/1.jpg","cheading":"Body Art","csubheading":"Tattoo, Piercing & More"},{"cid":"2","cimage_url":"http:\/\/fvilla.in\/app\/collections\/2.jpg","cheading":"Nail Care","csubheading":"Manicure, Nail Art & More"},{"cid":"3","cimage_url":"http:\/\/fvilla.in\/app\/collections\/3.jpg","cheading":"Hair Care","csubheading":"Straightening, Rebonding & More"},{"cid":"4","cimage_url":"http:\/\/fvilla.in\/app\/collections\/4.jpg","cheading":"Bridal Box","csubheading":"Bridal Makeup, Hair & More"},{"cid":"5","cimage_url":"http:\/\/fvilla.in\/app\/collections\/5.jpg","cheading":"Face Care","csubheading":"Facial, Makeup & More"},{"cid":"6","cimage_url":"http:\/\/fvilla.in\/app\/collections\/6.jpg","cheading":"Mehandi Show","csubheading":"Mehandi For Hand & Feet"}]

but I want the output in this format:

{
   "slider1" : [{"id":"3","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""},{"id":"6","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""}],
   "slider2" : [{ },{ }]
}
share|improve this question
    
what happens to the data from the second loop? is that to be part of the same json object or a different one? – RamRaider Sep 13 '15 at 10:50
    
it should be a part of same jason object!!! @RamRaider – Ankit Singh Sep 13 '15 at 11:22

Well, you can create your own array to hold your results from one table.

<?php  
$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select * from slider');  
$row->execute();//execute the query  
$json_data=array();//create the array
$result = array();
foreach($row as $rec)//foreach loop  
{  
    $json_array['id']=$rec['id'];  
    $json_array['image_url']=$rec['image_url'];  
    $json_array['link_image']=$rec['link_image'];  
    array_push($json_data,$json_array);  

}  
$result["table1"] = $json_data;

$row1=$db->prepare('select * from collections');  
$row1->execute();//execute the query  
$json_data1=array();//create the array  
foreach($row1 as $rec1)//foreach loop  
{  
    $json_array1['cid']=$rec1['id'];  
    $json_array1['cimage_url']=$rec1['image_url'];  
    $json_array1['cheading']=$rec1['heading']; 
    $json_array1['csubheading']=$rec1['subheading'];  
    array_push($json_data1,$json_array1);  
}
$result['table2'] = $json_data1;
echo json_encode($result);  
?>
share|improve this answer

Not entirely sure that this will suffice, but something like this might be what you are after:

$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$output=array();

$row=$db->prepare('select * from slider');  
$row->execute();
$sliders=array();
foreach( $row as $key => $rec ){  
    $sliders[ 'slider'.$key ]=array('id' => $rec['id'],'image_url' => $rec['image_url'],'link_image' => $rec['link_image']);
} 
$output['sliders']=$sliders;
#echo json_encode( $sliders );  



$row=$db->prepare('select * from collections');  
$row->execute();
$collections=array();
foreach( $row as $key => $rec ){  
    $collections [ 'collections'.$key ]=array('cid' => $rec['id'],'cimage_url' => $rec['image_url'],'cheading' => $rec['heading'],'csubheading' => $rec['subheading'] );
}
$output['collections']=$collections;
#echo json_encode( $collections );

echo json_encode( $output );
share|improve this answer
    
this is not working @ramraider i want to combine two array into json format like below.{ "slider1" : [{ },{ }], // first for slider table "slider2" : [{ },{ }] } // second for collection table – Ankit Singh Sep 13 '15 at 11:25

You can use UNION ALL to get all data from both tables in a single query i.e.

$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select id as slider_id, image_url as slider_image, heading, subheading from slider UNION ALL select id as header_id, image_url as header_image from collections');  
$row->execute();//execute the query  
$result = $row->fetchAll(PDO::FETCH_ASSOC);
$json_array = array();
foreach($result as $rec)//foreach loop  
{  
    $json_array['slider1']['id']=$rec['slider_id'];  
    $json_array['slider1']['image_url']=$rec['slider_image'];  
    $json_array['slider1']['link_image']=$rec['link_image'];   

    $json_array['slider2']['cid']=$rec['header_id'];  
    $json_array['slider2']['cimage_url']=$rec['header_image'];  
    $json_array['slider2']['cheading']=$rec['heading']; 
    $json_array['slider2']['csubheading']=$rec['subheading'];  

}
echo json_encode($json_array);

NOTE:-

Use UNION ALL to collect all data from two queries, since you have id, image_url common fields in both table so use alias as with different key as mentioned in query

Hope this works, Happy coding...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.