0

Edit: Solved using the below suggestion of removing:echo '{"results":'.json_encode($arr).'}'; and replacing with echo json_encode($arr);

This combined with replacing these lines:

var items = [];
        $.each(data, function(key, val) {
            $.each(val, function(key, val)
            {
                    //Test insert into box using customer number
                $("#chatbox").html(val.number);
            });
        });

with

$.each(data, function(key, val) {
$("#chatbox").html(this.number);
});

Made it work, I can now access the object data using the this followed by the property I want.

End Edit

Hi could anyone help me with these JSON feed. I am almost there with it (I think) however whenever I put this feed into JQuery it is picked us as a string rather than as an array of JSON objects and I can't figure out why.

The PHP to create the feed:

<?php
session_start();

include 'connect.php';

    $number = $_SESSION['number'];

    $query = "SELECT * FROM $tableName WHERE number='$number'";

    $result = mysql_query($query, $sqlCon);

    $arr = array();
    while($obj = mysql_fetch_object($result)) {
        $arr[] = $obj;
    }

    if (count($arr) == 0)
    {
        echo "Your reference number is: $number";
    } else {
        echo '{"results":'.json_encode($arr).'}';
    }
?>

The returned JSON looks like this:

{"results":[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]}

What I instead want is:

[{"id":"40","number":"466741","message":"dsfdsv","date":"2011-10-05","time":"00:28:32"},{"id":"41","number":"466741","message":"sacsac","date":"2011-10-05","time":"00:30:17"}]

Or a return value which would allow me to iterate over the objects.

The JQuery I'm reading it with:

$.getJSON('get.php', function(data) {

var items = [];
    $.each(data, function(key, val) {
        $.each(val, function(key, val)
        {
                //Test insert into box using customer number
            $("#chatbox").html(val.number);
        });
    });
});

I'm guessing my problem is the way in which I'm creating the JSON feed but I'm just stuck and cant figure out how to fix it.

Any help would be appreciated, thanks.

2
  • 1
    replace echo '{"results":'.json_encode($arr).'}'; with echo json_encode($arr); Commented Oct 5, 2011 at 0:42
  • Edit: ok scrap that: it does indeed work, how do I then pull out for example the number value? Commented Oct 5, 2011 at 0:43

1 Answer 1

2

You should set the headers from PHP to specify that it is json content. This isn't 100% required but good practice and you'll see the content-type parsed correctly in tools like firebug.

header('Content-type: application/json');

You are wrapping your json content with a results key that that you aren't looking for in your javascript so you either need to remove that part or look for that your javascript. Since you say you want the array and not the object with the results key/value just replace echo '{"results":'.json_encode($arr).'}'; with echo json_encode($arr);

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

2 Comments

Yeah I do want the array so that I can then loop over it (I'm new to JS can you tell =P) and pull out for example the number value for each JSON object.
I believe application/json is only supported by newer browsers. Older browsers may do bad things with that content type.

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.