1

my php code looks like:

$query = "SELECT *
        FROM `address`
        WHERE customer_id =$customer_id
        ORDER BY `default` DESC";

$result=mysql_query($query);

$value = mysql_num_rows($result);

if($value>=1)
{
    while($row = mysql_fetch_array($result)) 
    {

        $details =  array(
        'status'=>'sucess', 
        'message'=>'address available',
        'id' => $row['id'], 
        'customer_id' =>$row['customer_id'],
        'at' => $row['at'],
        'name'=>$row['name'],
        'mobile'=>$row['mobile'],
        'city'=>$row['city'],
        'address'=>$row['address'],
        'latlog'=>$row['latlog'],
        'default'=>$row['default']
        );          
    }
    echo  json_encode($details);
}

Its output looks like:

{"status":"sucess","message":"address available","id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}

it is json object. My require json is:

{"status":"sucess","message":"address available","details":[{"id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}]
} 

What all changes should I done to required json format? that is json object contain json array. what all changes should I done for getting required json format. I am new to this. Thanking in advance.

5 Answers 5

1

You will need to create an array outside to append result data and then put that in main array just before decoding it to JSON.

Like this,

if($value>=1)
{
    $details=array();
    while($row = mysql_fetch_array($result)) 
    {

        $details[]=  array(
        'id' => $row['id'], 
        'customer_id' =>$row['customer_id'],
        'at' => $row['at'],
        'name'=>$row['name'],
        'mobile'=>$row['mobile'],
        'city'=>$row['city'],
        'address'=>$row['address'],
        'latlog'=>$row['latlog'],
        'default'=>$row['default']
        );          
    } 
    $main_array=array();
    $main_array['status']='sucess';
    $main_array['message']='address available';
    $main_array['details']=$details;

    echo  json_encode($main_array);
}

This will generate JSON like this, http://json-parser.com/deec3edd

Sign up to request clarification or add additional context in comments.

Comments

1

What you want to do is to generate the details array and add it to the response as an array, that way you can control your status outside of the loop (if it returns no data for example):

if($value>=1){
    $details=array();
    while($row = mysql_fetch_array($result)) 
    {

        $details[]=  array(
        'id' => $row['id'], 
        'customer_id' =>$row['customer_id'],
        'at' => $row['at'],
        'name'=>$row['name'],
        'mobile'=>$row['mobile'],
        'city'=>$row['city'],
        'address'=>$row['address'],
        'latlog'=>$row['latlog'],
        'default'=>$row['default']
        );          
    } 
    $main_array=array();
    $message = 'address available';
}else{
    $message = 'no address';
    $details = null;
}

echo json_encode(array(
  'success' => $value>=1,
  'message' => $message, 
  'details' => $details
));

Comments

-1

Please remove while i think no need of while

$query ="SELECT id,customer_id,at,name,mobile,
        city,address,latlog,default
        FROM `address`
        WHERE customer_id =$customer_id
        ORDER BY `default` DESC";

$result=mysql_query($query);

$value = mysql_num_rows($result); 

$json=array();
        $json['status']='sucess';
        $json['message']='address available';
        $json['details']=$value;

        echo jsone_encode($json);

Comments

-1

Replace this code with your code ...its work for you...thanks

while($row = mysql_fetch_array($result)) 
{

    $details =  array(

    'id' => $row['id'], 
    'customer_id' =>$row['customer_id'],
    'at' => $row['at'],
    'name'=>$row['name'],
    'mobile'=>$row['mobile'],
    'city'=>$row['city'],
    'address'=>$row['address'],
    'latlog'=>$row['latlog'],
    'default'=>$row['default']
    );          
}
$json= array('status'=>'sucess','message'=>'address available','details'=> $details);
echo  json_encode($json);

Comments

-1

you can edit your code

while($row = mysql_fetch_array($result)) 
{

    $details =  array(
    'status'=>'sucess', 
    'message'=>'address available',
    'id' => $row['id'], 
    'customer_id' =>$row['customer_id'],
    'at' => $row['at'],
    'name'=>$row['name'],
    'mobile'=>$row['mobile'],
    'city'=>$row['city'],
    'address'=>$row['address'],
    'latlog'=>$row['latlog'],
    'default'=>$row['default']
    );          
}
echo  json_encode($details);

to

$datails = array();
while($row = mysql_fetch_array($result)) 
{
  $detail =  array(
  'status'=>'sucess', 
  'message'=>'address available',
  'details'=>[
    'id' => $row['id'], 
    'customer_id' =>$row['customer_id'],
    'at' => $row['at'],
    'name'=>$row['name'],
    'mobile'=>$row['mobile'],
    'city'=>$row['city'],
    'address'=>$row['address'],
    'latlog'=>$row['latlog'],
    'default'=>$row['default']
 ]
 );
 array_push($datails,$datail);         
}
echo  json_encode($details);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.