I need to display comments with responds from database because i need asynchronous connection i need to transfer mysql output for json array.
So, i have two tables in my DB, one cold comments and second subcomment. Code i use for display
$sql = "SELECT comments.* , subcomment.comment AS subcom FROM comments left join subcomment on comments.id = subcomment.forid ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
} else {
echo "0 results";
}
But that code generate me json with multiply comments, so if one comment have 10 responds my output will have 10 comments with 10 respond, looking like that:
[{
"id":583,
"user_uid":"xxx",
"video_id":"stackoverflow",
"comment":"what did you try?",
"created":"2019-02-19 11:43:15",
"subcom":"nothing special"
},{
"id":583,
"user_uid":"xxx",
"video_id":"stackoverflow",
"comment":"what did you try?",
"created":"2019-02-19 11:43:15",
"subcom":"my sql commands"
},{
"id":583,
"user_uid":"xxx",
"video_id":"stackoverflow",
"comment":"what did you try?",
"created":"2019-02-19 11:43:15",
"subcom":"php? or json"}]
What do i need? Something like that:
{
"id":583,
"user_uid":"xxx",
"video_id":"stackoverflow",
"comment":"what did you try?",
"created":"2019-02-19 11:43:15",
"subcom":{
sub1: "nothing special" ,
sub2: "my sql commands" ,
sub3: "php? or json"
}}
Or:
{
"id":583,
"user_uid":"xxx",
"video_id":"stackoverflow",
"comment":"what did you try?",
"created":"2019-02-19 11:43:15",
"subcom":["nothing special" , "my sql commands" , "php? or json"]
}
I will be glad if someone will show me the way to do it :D