user1853803
user1853803

Reputation: 659

Php json generation for multiple records

I am trying to create a json using php

while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC)) 
{ 
    $id          = stripslashes($info['id']); 
    $pricedol    = stripslashes($info['pricedol']); 
    $final_result = array('id' => $id,'pricedol' => $pricedol );                             
} 

$json = json_encode($final_result);
echo $json;

This is giving the following output

{
    "id": 14567,
    "pricedol": 15.57
}

But i have couple of records in the db...i want the following output

 {
    "id": 14567,
    "pricedol": 15.57
},
{
    "id": 4567,
    "pricedol": 55.25
},

One more thing...do i need to serialize before parsing in jquery

Upvotes: 0

Views: 50

Answers (1)

vee
vee

Reputation: 38645

You could create a multi-dimensional array and use json_encode on that array:

$final_result = array();
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC)) 
{ 
    $id          = stripslashes($info['id']); 
    $pricedol    = stripslashes($info['pricedol']); 
    $final_result[] = array('id' => $id,'pricedol' => $pricedol);                             
} 

$json = json_encode($final_result);
echo $json;

Upvotes: 2

Related Questions