我试图让我的 html 'commentlinktoggle' 与我的帖子数组的其余部分一起通过。昨天我开始学习 Json 并且在大多数情况下我做得还不错。但我正在尝试了解 array_push,因为我认为这是我需要传递这个不在我的数据库 $row 循环中的变量。所以这就是我所拥有的和我需要做的。
这是我想退出帖子的 json 响应
{
"posts": {
"sharedcontent_userstimestamp": "<div id='time' title='Posted on Thursday 01 Jan 1970 at 01:00AM '>⌚2017 years <\/div>",
"streamitem_timestamp": "<div id='time' title='Posted on Thursday 23 Feb 2017 at 07:55PM '>⌚5 days <\/div>",
"1": {
"streamitem_id": "1178",
"streamitem_content": "Uploaded a new picture<hr>To Album: <a href=photolist.php?uid=3&aid=16>tee<\/a><br\/><br\/>tee",
"streamitem_type_id": "4",
"streamitem_timestamp": "2016-12-11 02:58:52",
"streamitem_collect": "<li><a id=\"collectpostlink1178\"
"commentlinktoggle": "<div id='streamcomment' style='cursor:pointer;display:block;' class='toggleon1178'><a style='cursor:pointer;' id='commenttoggle_1178' onclick=\"toggle_comments('comment_holder_1178');swapcommentlabel(this.id);\"><div id='loadcommentcount1178'>Comments 1<\/div><\/a><\/div>"
},
但它是这样出来的。与 commentlinktoggle 清楚地在帖子数组之外。
{
"posts": {
"sharedcontent_userstimestamp": "<div id='time' title='Posted on Thursday 01 Jan 1970 at 01:00AM '>⌚2017 years <\/div>",
"streamitem_timestamp": "<div id='time' title='Posted on Thursday 23 Feb 2017 at 07:55PM '>⌚5 days <\/div>",
"1": {
"streamitem_id": "1178",
"streamitem_content": "Uploaded a new picture<hr>To Album: <a href=photolist.php?uid=3&aid=16>tee<\/a><br\/><br\/>tee",
"streamitem_type_id": "4",
"streamitem_timestamp": "2016-12-11 02:58:52",
"streamitem_collect": "<li><a id=\"collectpostlink1178\" style=\"cursor:pointer;color:#000;\" onclick=\"collectpost(2309);\">Collect<\/a><\/li>",
}
},
"count": 9,
"commentlinktoggle": "<div id='streamcomment' style='cursor:pointer;display:block;' class='toggleon2309'><a style='cursor:pointer;' id='commenttoggle_2309' onclick=\"toggle_comments('comment_holder_2309');swapcommentlabel(this.id);\"><div id='loadcommentcount2309'>Comments 3<\/div><\/a><\/div>"
}
这是我尝试使用 array_push 推送的 php 代码。我知道我把它弄得一团糟。但是,如果有人能解释我哪里出错了,这对我将来参考会有很大帮助。
$rowcount = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$json = array(
'posts' => array(),
'count' => $rowcount,
'commentlinktoggle' => $sendcommentlinktoggle,
);
$sendcommentlinktoggle = "Some html in here";
array_push($posts, array('commentlinktoggle' => ));
$posts[] = array(
//Post information and ids
'streamitem_id' => $row['streamitem_id'],
'streamitem_content' => $row['streamitem_content'],
/and so on...
);
$rowcount++;
}
$json['posts'] = $posts;
echo json_encode($json);
也许这也有帮助,我的函数循环,我在其中附加我的数据
success: function (data) {
$.each(data.posts, function(i, response) {
$("#homestatusid").append(""+response['commentlinktoggle']+"");
)};
}
如果有人需要更多信息,请询问,我会尽力提供帮助。
您正在推送$posts
而不是推送到您稍后添加的元素,$post[]
因此这就是将元素添加到单个帖子之外的原因,在同一级别但具有关联键。
最简单的解决方案是在添加其余内容的位置添加元素:
$posts[] = array(
//Post information and ids
'streamitem_id' => $row['streamitem_id'],
'streamitem_content' => $row['streamitem_content'],
//and so on...
// Add the additional content
'commentlinktoggle' => $sendcommentlinktoggle,
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句