将 html 推送到已经创建的 json 数组中

闸道

我试图让我的 html 'commentlinktoggle' 与我的帖子数组的其余部分一起通过。昨天我开始学习 Json 并且在大多数情况下我做得还不错。但我正在尝试了解 array_push,因为我认为这是我需要传递这个不在我的数据库 $row 循环中的变量。所以这就是我所拥有的和我需要做的。

这是我想退出帖子的 json 响应

{
    "posts": {
    "sharedcontent_userstimestamp": "<div id='time' title='Posted on Thursday  01 Jan 1970 at 01:00AM '>&#8986;2017 years <\/div>",
    "streamitem_timestamp": "<div id='time' title='Posted on Thursday  23 Feb 2017 at 07:55PM '>&#8986;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 '>&#8986;2017 years <\/div>",
    "streamitem_timestamp": "<div id='time' title='Posted on Thursday  23 Feb 2017 at 07:55PM '>&#8986;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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将JSON数据推送到数组中?

来自分类Dev

将新项目推送到JSON数组中

来自分类Dev

将JSON数据推送到Jquery数组

来自分类Dev

如何将html按钮(输入标签)作为json数组中的json对象推送

来自分类Dev

将数据推送到json对象中

来自分类Dev

将具有未知路径的嵌套 Json 值推送到 Javascript 中的数组中

来自分类Dev

在将JSON对象推送到Javascript之前过滤数组

来自分类Dev

在将JSON对象推送到Javascript之前过滤数组

来自分类Dev

ReactJS-将JSON推送到状态数组

来自分类Dev

在将JSON对象推送到Javascript之前过滤数组

来自分类Dev

Javascript将JSON推送到数组(高图)

来自分类Dev

使用大括号将元素推送到 JSON 数组

来自分类常见问题

如何将数据推送到JSON文件中的数组并保存?

来自分类Dev

如何将JSON对象推送到JSONB列中的嵌套数组

来自分类Dev

如何将数据推送到JSON文件中的数组并保存?

来自分类Dev

将JSON数据推送到Angular JS中的现有数组

来自分类Dev

推送到madrill JSON数组中

来自分类Dev

将对象推送到 JSON 数组中

来自分类Dev

如何将json值推送到数组中而不会在角度6中被替换?

来自分类Dev

将子数组推送到数组中的特定键

来自分类Dev

将Angular数组数据推送/追加到HTML元素中?

来自分类Dev

将内容推送到数组

来自分类Dev

使用codeigniter php将数据推送到数组中

来自分类Dev

无法使函数将数组推送到NodeJS中的redis列表

来自分类Dev

使用php将键值推送到空数组中

来自分类Dev

将项目推送到数组中并以javascript返回

来自分类Dev

Ajax回发后将项目推送到数组中

来自分类Dev

无法将子文档推送到Mongoose中的文档数组

来自分类Dev

如何按星期将日期推送到数组中?

Related 相关文章

热门标签

归档