Jump to content

how to make proper json data in php


doforumda

Recommended Posts

I am trying to make json data in php using json_encode() function. Then I will use this data on the client side. I acheived some of the part. My current code displays json data in this format

 

{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc8f94f1a51c5.32653037",
    "name":"Zafar Saleem",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
        "created_at":"2012-06-02 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c413554104.22444656",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
        "created_at":"2012-06-03 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c48c529675.45551665",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
        "created_at":"2012-06-04 00:00:00",
        "seeked":"20"
    }
}
}

 

what I want to show above data in this form

 

{
"data": 
[
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc8f94f1a51c5.32653037",
        "name":"Zafar Saleem",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
            "created_at":"2012-06-02 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c413554104.22444656",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
            "created_at":"2012-06-03 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c48c529675.45551665",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
            "created_at":"2012-06-04 00:00:00",
            "seeked":"20"
        }
    }
]
}

 

here is my php code that generates json data

 

database function that gets data from database

 

public function getHome() {
    $result = mysql_query("SELECT * FROM places") or die(mysql_error());
    // check for result
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $data[] = $row;
        }
        return $data;
        /*
        $result = mysql_fetch_array($result);
        return $result;
        */
    } else {
        // user not found
        return false;
    }
}

 

here is where I make json on php

 

if($db->getHome()) {
        $data = $db->getHome();
        foreach($data as $r) {
            $response['success'] = 1;
            $response['uid'] = $r['uid'];
            $response['name'] = $r['name'];
            $response['profile_photo'] = $r['profile_photo_path'];
            $response['places']['place_photo'] = $r['place_photo_path'];
            $response['places']['latitude'] = $r['latitude'];
            $response['places']['longitude'] = $r['longitude'];
            $response['places']['created_at'] = $r['created_at'];
            $response['places']['seeked'] = $r['total_completed'];
            echo json_encode(array('data' => $response));
        }
    } else {
        $response['error'] = 1;
        $response['error_msg'] = 'No data available';
        echo json_encode($response);
    }

Link to comment
Share on other sites

if($db->getHome()) {
        $data = $db->getHome();
        $response = array('data' => array());
        foreach($data as $r) {
            array_push($response['data'], array(
                'success' => 1,
                'uid' => $r['uid'],
                'name' => $r['name'],
                'profile_photo' => $r['profile_photo_path'],
                'places' => array(
                    'place_photo' => $r['place_photo_path'],
                    'latitude' => $r['latitude'],
                    'longitude' => $r['longitude'],
                    'created_at' => $r['created_at'],
                    'seeked' => $r['total_completed'],
                )
            ));
        }
    } else {
        $response = array('error' => 1, 'error_msg' => 'No data available');
    }
    echo json_encode($response);

Link to comment
Share on other sites

By the way... this is HORRIBLY inefficient... will execute your query twice:

 

 

if($db->getHome()) {
    $data = $db->getHome();

 

Instead, do this... so you execute your query only once:

 

$data = $db->getHome();
if(!empty($data)) {

 

Cheers

Link to comment
Share on other sites

if($db->getHome()) {
        $data = $db->getHome();
        $response = array('data' => array());
        foreach($data as $r) {
            array_push($response['data'], array(
                'success' => 1,
                'uid' => $r['uid'],
                'name' => $r['name'],
                'profile_photo' => $r['profile_photo_path'],
                'places' => array(
                    'place_photo' => $r['place_photo_path'],
                    'latitude' => $r['latitude'],
                    'longitude' => $r['longitude'],
                    'created_at' => $r['created_at'],
                    'seeked' => $r['total_completed'],
                )
            ));
        }
    } else {
        $response = array('error' => 1, 'error_msg' => 'No data available');
    }
    echo json_encode($response);

 

Thanks. it works :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.