I am performing a query which returns a list of comments as shown below:
$sQuery = "
select cp_comments.*,users.user_login, users.user_url, users.display_name, users.ID as avatar, cp_comments.id as replies
from ".$wpdb->prefix."cp_comments cp_comments
left join ".$wpdb->prefix."users users on users.ID=cp_comments.uid
where songid='$id'
order by cp_comments.id asc
";
$result1 = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
I then loop through the results and encode a json array like so:
/*
* Output
*/
$output = array(
"comments" => array()
);
while ( $aRow = mysql_fetch_array( $result1, MYSQL_ASSOC ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "playtime" )
{
/* Special output formatting for 'playtime' column */
$row[$aColumns[$i]] = ($aRow[ $aColumns[$i] ]) / $duration * 100;
}
else if ( $aColumns[$i] == "avatar" )
{
/* Special output to render Avatar by user id */
$row[$aColumns[$i]] = commentplayer_get_user_avatar($aRow[ $aColumns[$i] ]);
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[$aColumns[$i]] = $aRow[ $aColumns[$i] ];
}
}
$output['comments'][] = $row;
}
echo json_encode($output);
This produces a JSON response as shown:-
{ "comments" : [ { "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/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test comment",
"display_name" : "admin",
"id" : "70",
"playtime" : 29.597701149399999,
"posttime" : "2011-10-13 15:51:01",
"cid" : "70",
"songid" : "30",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "Another genius comment....",
"display_name" : "admin",
"id" : "71",
"playtime" : 48.8505747126,
"posttime" : "2011-10-13 15:55:38",
"cid" : "71",
"songid" : "30",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
] }
However I need to retrieve the ‘replies’ to each comment (which are stored in a seperate table and nest them within the same JSON object.
So that the final encoded JSON is output like so:-
{ "comments" : [ { "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"
}]
},
I can’t seem to work out how to nest the results of a second mysql query into each row (assuming any replies exist at all)
The query I would like to run to return the replies for a given comment (linked as ‘cid’ in the two tables) is:-
$sql = "select cp_replies.*,users.user_login, users.user_url, users.display_name, users.ID as avatar
from ".$wpdb->prefix."cp_replies cp_replies
left join ".$wpdb->prefix."users users on users.ID=cp_replies.uid
where cp_replies.cid= $cid
order by cp_replies.id asc";
$result2 = mysql_query( $sql, $gaSql['link'] ) or die(mysql_error());
With the variable $cid needing to be passed dynamically to the query depending on which ‘cid’ of each row for th first query. (I hope that makes sense).
I tried creating a seperate function containing the query, passed it the ‘cid’ as variable but kept getting ‘null’ returned when I tried to retrieve the array.
I really am stuck here guys and truly hope someone can help me.
EDIt:
Having made the changes suggested by StuR, it is clear that I am getting closer but it’s still not as intended. My JSON (assuming I have implemented his suggestions correctly, is now as follows:-
{ "comments" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment at 0:48",
"display_name" : "admin",
"id" : "2",
"playtime" : 17.977528089900002,
"posttime" : "2011-09-28 14:38:41",
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "replies" : { "body" : "haha reply",
"cid" : "2",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin"
} },
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "asdasd",
"display_name" : "admin",
"id" : "3",
"playtime" : 0.74906367041199995,
"posttime" : "2011-09-28 14:43:11",
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "replies" : { "body" : "haha reply",
"cid" : "2",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin"
} },
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"display_name" : "admin",
"id" : "10",
"playtime" : 36.329588014999999,
"posttime" : "2011-10-06 14:15:12",
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
},
{ "replies" : { "body" : "haha reply",
"cid" : "2",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin"
} }
] }
Any builds on this?
This is creating a nested array under ‘comments’ for each row it loops, so before encoding it to JSON your array structure would look something like:
So you want to put your replies into:
What I would do is put your second query within your while loop, and then do this:
And change in your first while:
to