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

时间:2012-01-09 16:21:55

标签: php mysql json

我正在执行一个返回注释列表的查询,如下所示:

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

对此构建任何内容吗?

1 个答案:

答案 0 :(得分:1)

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