Note that there are some explanatory texts on larger screens.

plurals
  1. POusing two mySQL queries to create nested JSON array
    primarykey
    data
    text
    <p>I am trying to create a nested JSON array with the results of the second query being attached to the rows of the first.</p> <p>My code so far is as follows:-</p> <pre><code>$output = array(); $sql = "select cp_comments.*,users.user_login from ".$wpdb-&gt;prefix."cp_comments cp_comments left join ".$wpdb-&gt;prefix."users users on users.ID=cp_comments.uid where songid='$id' order by cp_comments.id asc"; $comments = $wpdb-&gt;get_results($sql); foreach($comments as $c){ $sql = "select cp_replies.*,users.user_login from ".$wpdb-&gt;prefix."cp_replies cp_replies left join ".$wpdb-&gt;prefix."users users on users.ID=cp_replies.uid where cp_replies.cid='".$c-&gt;id."' order by cp_replies.id asc"; $replies = $wpdb-&gt;get_results($sql); $output['comment-'.$c-&gt;id] = $c; if($replies){ foreach($replies as $r){ // The line below causes the problem //$output['comment-'.$c-&gt;id][] = $r; } } } echo json_encode( $output ); </code></pre> <p>As you can see what I am attempting to do is retrieve the results of query-1, lop through them and populate an array. So far so good. Then for each row in the resultset, perform a second query, using $c->id as a variable to dynamically change the second query dependant upon the id, then nest this data within each returned row of the first query.</p> <p>The line I have commented out causes an error:-</p> <pre><code>Fatal error: Cannot use object of type stdClass as array in </code></pre> <p>Although I have a rough understanding of why this error is happening, I do not know how to fix it and when I have tried a standard multidimensional array using while loops etc, I was then not able to access the $c->$id variable making the whole second query worthless.</p> <p>Essentially I would like the data returned in this format:-</p> <pre><code>{ "comment-2" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg", "body" : "More tests....", "display_name" : "admin", "id" : "26", "playtime" : 36.206896551699998, "posttime" : "2011-10-08 11:11:55", "cid" : "26", "songid" : "30", "uid" : "1", "user_login" : "admin", "user_url" : "http://www.songbanc.com/members/admin/" "cid": "1", "replies" : [ { "cid" : "26", "body" : "test reply", "posttime" : "2011-10-08 11:11:55" }] }, </code></pre> <p>It is currently 2 dimensional however without the 'replies'.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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