gpt4 book ai didi

php - 如何将第二个 mysql 的结果嵌套在 JSON 对象的每一行中?

转载 作者:行者123 更新时间:2023-11-29 14:37:10 25 4
gpt4 key购买 nike

我正在执行一个查询,该查询返回评论列表,如下所示:

$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());

然后我循环遍历结果并编码一个 json 数组,如下所示:

/*
* 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);

这会生成一个 JSON 响应,如下所示:-

{ "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/"
}
] }

但是,我需要检索每个评论的“回复”(这些回复存储在单独的表中,并将它们嵌套在同一个 JSON 对象中。

最终编码的 JSON 输出如下:-

{ "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"
}]

},

我似乎无法弄清楚如何将第二个 mysql 查询的结果嵌套到每一行中(假设任何回复都存在)

我想运行以返回给定评论(在两个表中链接为“cid”)的回复的查询是:-

$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());

变量 $cid 需要动态传递给查询,具体取决于第一个查询的每行的“cid”。 (我希望这是有道理的)。

我尝试创建一个包含查询的单独函数,将“cid”作为变量传递给它,但当我尝试检索数组时,不断返回“null”。

我真的被困在这里了,真心希望有人能帮助我。

编辑:

完成 StuR 建议的更改后,很明显我已经接近目标,但仍然没有达到预期。我的 JSON(假设我已经正确实现了他的建议,现在如下:-

{ "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"
} }
] }

在此基础上有任何构建吗?

最佳答案

$output['comments'][] = $row;

这会在循环的每一行的“comments”下创建一个嵌套数组,因此在将其编码为 JSON 之前,您的数组结构将类似于:

$output['comments'][]['id'] = 26;
$output['comments'][]['songid'] = 30;

所以你想将你的回复放入:

$output['comments'][]['replies'] = $replies;

我要做的是将第二个查询放入 while 循环中,然后执行以下操作:

while ( $replies = mysql_fetch_array( $result2, MYSQL_ASSOC ) )
{
$output['comments'][$cid]['replies'][] = $replies;
}

并在你的第一次改变:

$output['comments'][] = $row;

$output['comments'][$cid] = $row;

关于php - 如何将第二个 mysql 的结果嵌套在 JSON 对象的每一行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8791610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com