1

I have this JSON code:

{
    "phrases": [
        {
            "phrases": [
                {
                    "id": "33",
                    "text": "sasdsad",
                    "date": "2012-03-14 20:28:45",
                    "views": "0",
                    "ip": "64.191.90.5",
                    "reported": "0",
                    "strange": "0",
                    "lang": "en"
                },
                {
                    "id": "32",
                    "text": "que ondaa\r\n",
                    "date": "2012-03-14 20:27:45",
                    "views": "0",
                    "ip": "64.191.90.5",
                    "reported": "0",
                    "strange": "0",
                    "lang": "en"
                },
                {
                    "id": "31",
                    "text": "dsadssadsad",
                    "date": "2012-03-14 20:27:35",
                    "views": "0",
                    "ip": "64.191.90.5",
                    "reported": "0",
                    "strange": "0",
                    "lang": "en"
                }
            ],
            "details": {
                "success": "true",
                "phrase_id": "",
                "phrase_text": "",
                "phrase_date": ""
            }
        }

I don't really know what to do. I get some phrases vía MySQL, and pushes them to an array. This array is json_encoded() and gets printed.

$sth = $sql;
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
}
$sth = $sql;
$data = array(
    "success" => "true", 
    "phrase_id"      => "",
    "phrase_text"      => "",
    "phrase_date"      => "",
);
    print json_encode($rows).json_encode($data);

and with jQuery I was trying to parse it, but I can't. This is the main problem.

function getPhrases(order,limit,last){
    var req_url = ...;
    $.getJSON(req_url, function(data) {
        $.each(data.phrases, function(i, data) {
            appendPhrase(data.text);
            lastid = data.id;
        });
        $.each(data.details, function(i, data) {
            $("#phrases-count").html(data.totalcount);
        });
    });
}

PS: I was doing this with "echo" but got some problems.

{
    "phrases": [
        {
            "id": "33",
            "text": "sasdsad",
            "date": "2012-03-14 20:28:45",
            "views": "0",
            "ip": "64.191.90.5",
            "lang": "en"
        },
        {
            "id": "32",
            "text": "que ondaa<br />",
            "date": "2012-03-14 20:27:45",
            "views": "0",
            "ip": "64.191.90.5",
            "lang": "en"
        },
        {
            "id": "31",
            "text": "dsadssadsad",
            "date": "2012-03-14 20:27:35",
            "views": "0",
            "ip": "64.191.90.5",
            "lang": "en"
        }
    ],
    "details": [
        {
            "totalcount": "3",
            "logged_in": "false"
        }
    ]
}

1 Answer 1

2

You can't simply combine these JSON arrays:

print json_encode($rows).json_encode($data);

Try this (attempt 2):

print json_encode( array('phrases' => $rows, 'details' => $data) );
Sign up to request clarification or add additional context in comments.

4 Comments

Just in my question, see the last part.
Please add the JSON structure you want to get.
Sorry, the JSON is yet different. Also need to fetch it thru jQuery. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.