通过php

时间:2017-07-16 07:22:23

标签: php mysql json

我有两张桌子;一个用于图片,另一个用于他们的评论(l

picture_table (pic_id,pic_owner,pic_insert_date)

picture_comment_table (pic_id,pic_commentor_name,pic_comments,pic_comment_date)

如何创建一个PHP代码,在所有图片的特定图片对象中创建一个注释数组,如下面的代码?

[  
    {
        "pic_id": "picture100",
        "pic_owner": "Stanley Kin",
        "pic_insert_date": "23-02-2017",   
        [
            {
                "pic_commentor_name": "Kim Love",
                "pic_comments": "You look beautifull"
                "pic_comment_date": "31-01-2017"
            }, 
            {
                "pic_commentor_name": "John doe Love",
                "pic_comments": "Marvelous you are"
                "pic_comment_date": "13-01-2017"
            }
        ]   
    },   
    { 
        "pic_id": "picture101",
        "pic_owner": "Linky Nam",
        "pic_insert_date": "21-04-2017",   
        [
            {
                "pic_commentor_name": "Lira Love",
                "pic_comments": "You look Gorgeous"
                "pic_comment_date": "31-04-2017"
            },
            {
                "pic_commentor_name": "Bram Javan",
                "pic_comments": "love the pics more....."
                "pic_comment_date": "11-01-2017"
            }
        ]  
    }
]

2 个答案:

答案 0 :(得分:0)

您不必知道从mysql表创建json的列。 我在下面创建了简单的代码,将表格带到json。 注意: 解析时通过'comments'键获取图片的注释; 假设conn变量已被定义。

public function getPictureInfos(){
    $pictures = $this->getNewDataFromTable("picture_table");

    for($i = 0; $i < count($pictures); $i++) {
        $pictures[$i]['comments'] = $this->getNewDataFromTable("picture_comment_table");
    }
    return json_encode($pictures);
}

public function getNewDataFromTable($tableName){
    $sql = "SELECT * FROM ".$tableName;
    $result=$this->conn->query($sql);
    $data = array();
    $count=0;
    if($result)
    while($row = $result->fetch_assoc()) {  
        $data[$count]= $row;
        $count++;
    }
    return $data;
}

编辑:使用“评论”键接收评论的含义。

举一个你的json字符串的例子:

[
{ "pic_id": "picture100", "pic_owner": "Stanley Kin", "pic_insert_date": "23-02-2017",
[ { "pic_commentor_name": "Kim Love", "pic_comments": "You look beautifull" "pic_comment_date": "31-01-2017" }, { "pic_commentor_name": "John doe Love", "pic_comments": "Marvelous you are" "pic_comment_date": "13-01-2017" } ]
} ]

其中注释数组是包含图片详细信息的对象的成员之一。数组没有键,你的json实际上会在解析过程中抛出JSONException。

要解决这个问题,请在创建json时为该数组提供一个键,并在解析时通过'comments'键对图片进行注释,

所以我的代码生成的json就是这样:

[
{ "pic_id": "picture100", "pic_owner": "Stanley Kin", "pic_insert_date": "23-02-2017",
"comments":[ { "pic_commentor_name": "Kim Love", "pic_comments": "You look beautifull" "pic_comment_date": "31-01-2017" }, { "pic_commentor_name": "John doe Love", "pic_comments": "Marvelous you are" "pic_comment_date": "13-01-2017" } ]
} ]

答案 1 :(得分:0)

我认为这就是你的意思

<?php
$pic[0] = array("pic_id"=> "picture100","pic_owner"=> "Stanley Kin","pic_insert_date"=> "23-02-2017",);
$pic[0]['comments'][] = array("pic_commentor_name"=> "Kim Love","pic_comments"=> "You look beautifull","pic_comment_date"=> "31-01-2017");
$pic[0]['comments'][] = array("pic_commentor_name"=> "John doe Love","pic_comments"=> "Marvelous you are","pic_comment_date"=> "31-01-2017");
$pic[1] = array("pic_id"=> "picture101","pic_owner"=> "Linky Nam","pic_insert_date"=> "21-04-2017");
$pic[0]['comments'][] = array("pic_commentor_name"=> "Lira Love","pic_comments"=> "You look Gorgeous","pic_comment_date"=> "31-01-2017");
$pic[0]['comments'][] = array("pic_commentor_name"=> "Bram Javan","pic_comments"=> "love the pics more.....","pic_comment_date"=> "11-01-2017");
$json = json_encode($pic);
echo $json;
?>

结果:

[
    {
        "pic_id": "picture100",
        "pic_owner": "Stanley Kin",
        "pic_insert_date": "23-02-2017",
        "comments": [
            {
                "pic_commentor_name": "Kim Love",
                "pic_comments": "You look beautifull",
                "pic_comment_date": "31-01-2017"
            },
            {
                "pic_commentor_name": "John doe Love",
                "pic_comments": "Marvelous you are",
                "pic_comment_date": "31-01-2017"
            }
        ]
    },
    {
        "pic_id": "picture101",
        "pic_owner": "Linky Nam",
        "pic_insert_date": "21-04-2017",
        "comments": [
            {
                "pic_commentor_name": "Lira Love",
                "pic_comments": "You look Gorgeous",
                "pic_comment_date": "31-01-2017"
            },
            {
                "pic_commentor_name": "Bram Javan",
                "pic_comments": "love the pics more.....",
                "pic_comment_date": "11-01-2017"
            }
        ]
    }
]

对于SQL使用此:

$con=@mysql_connect('host', 'user', 'password');
$query = "SELECT * FROM `picture_table`";
$result=mysql_query($query,$con);
$pic = array();
$counter = 0;
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
    $pic[$counter] = array("pic_id"=> $row["pic_id"],"pic_owner"=> $row["pic_owner"],"pic_insert_date"=> $row["pic_insert_date"]);
    $query = "SELECT * FROM `picture_comment_table`";
    $result=mysql_query($query,$con);
    while($comments_row=mysql_fetch_array($result,MYSQL_ASSOC)){
        $pic[$counter]['comments'][] = array("pic_commentor_name"=> $comments_row['pic_commentor_name'],"pic_comments"=> $comments_row['pic_comments'],"pic_comment_date"=> $comments_row['pic_comment_date']);
    }
    $counter++;
}

或者这个:

$con=@mysql_connect('host', 'user', 'password');
$query = "SELECT `picture_table`.*
                , GROUP_CONCAT(DISTINCT CONCAT_WS( '^^^^',`picture_comment_table`.`pic_commentor_name` , `picture_comment_table`.`pic_comments` , `picture_comment_table`.`pic_comment_date`)SEPARATOR '||||' ) AS `comment`
            FROM `picture_table` 
            INNER JOIN `picture_comment_table` USING (`pic_id`)
            GROUP BY `pic_id`";
$result=mysql_query($query,$con);
$pic = array();
$counter = 0;
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
    $pic[$counter] = array("pic_id"=> $row["pic_id"],"pic_owner"=> $row["pic_owner"],"pic_insert_date"=> $row["pic_insert_date"]);
    $comment_list = explode("||||",$row['comment']);
    foreach ($comment_list as $value){
        $comment_array = explode("^^^^",$value);
        $pic[$counter]['comments'][] = array("pic_commentor_name"=> $comment_array[0],"pic_comments"=> $comment_array[1],"pic_comment_date"=> $comment_array[2]);
    }
    $counter++;
}