Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a JSON feed using PHP For Use With JQuery
    text
    copied!<p><strong>Edit:</strong> Solved using the below suggestion of removing:<code>echo '{"results":'.json_encode($arr).'}';</code> and replacing with <code>echo json_encode($arr);</code> </p> <p>This combined with replacing these lines: </p> <pre><code>var items = []; $.each(data, function(key, val) { $.each(val, function(key, val) { //Test insert into box using customer number $("#chatbox").html(val.number); }); }); </code></pre> <p>with </p> <pre><code>$.each(data, function(key, val) { $("#chatbox").html(this.number); }); </code></pre> <p>Made it work, I can now access the object data using the <code>this</code> followed by the property I want.</p> <p><strong>End Edit</strong></p> <p>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.</p> <p><strong>The PHP to create the feed:</strong></p> <pre><code>&lt;?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).'}'; } ?&gt; </code></pre> <p><strong>The returned JSON looks like this:</strong></p> <pre><code>{"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"}]} </code></pre> <p><strong>What I instead want is:</strong></p> <pre><code>[{"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"}] </code></pre> <p>Or a return value which would allow me to iterate over the objects.</p> <p><strong>The JQuery I'm reading it with:</strong></p> <pre><code>$.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); }); }); }); </code></pre> <p>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.</p> <p>Any help would be appreciated, thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload