如何使用数据库中这两个表的内容创建JSON对象?

时间:2017-10-07 12:12:07

标签: php json

这是我想要的格式:

{
    "category": "ballet instructor",
    "category_id": 182,
    "name": "bob",
    "phone": "12345678",
    "address": "Poland",
    "comment": "she is fantastic",
    "checkedcontacts": [{
            "checkedcontact": 23
        },
        {
            "checkedcontact": 33
        }
    ]
}

这就是我得到的:

{
    "category": "ballet instructor",
    "category_id": 182,
    "name": "bob",
    "phone": "12345678",
    "address": "Poland",
    "comment": "she is fantastic",
    "checkedcontact": 23
        }

我对JSON表的review输出没有问题,review_shared表导致了我的问题。对于checkedcontact,我如何获得数组?因为现在我的JSON对象中只出现了checkedcontact的一个值,即使表中可能有多个值。

这是我的代码:

<?php

require('dbConnect.php');

//this is the review_id clicked in the ListView
$Review_id = $_POST['review_id'];
//$Number = "51";
// The ? below are parameter markers used for variable binding
// auto increment does not need prepared statements

// get the review_id in the review table, then get the matching fields in the row 
                $query = "SELECT * FROM review WHERE review_id = ?";
                $stmt = $con->prepare($query) or die(mysqli_error($con));
                $stmt->bind_param('i', $Review_id) or die ("MySQLi-stmt binding failed ".$stmt->error);
                $stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
                $result = $stmt->get_result();


//In review_shared table let's get the review_id and then the matching contact_id in those rows 
                $query2 = "SELECT * FROM review_shared WHERE review_id = ?";
                $stmt2 = $con->prepare($query2) or die(mysqli_error($con));
                $stmt2->bind_param('i', $Review_id) or die ("Review_shared, MySQLi-stmt binding failed ".$stmt2->error);
                $stmt2->execute() or die ("Review_shared, MySQLi-stmt execute failed ".$stmt2->error);
                $result2 = $stmt2->get_result();                

                //set up the object called Review
                class Review {

                    public $category = "";
                    public $category_id = "";
                    public $name = "";
                    public $phone = "";
                    public $address = "";
                    public $comment = "";

                    public $checkedcontact = "";

                }

                $review = new Review();

            while($row = mysqli_fetch_array($result)) {
                //get the corresponding fields in the review_id row
                //make it into a json object
            $review -> category = $row["cat_name"];
            $review -> category_id = $row["cat_id"];
            $review -> name = $row["name"];
            $review -> phone = $row["phone"];
            $review -> address = $row["address"];
            $review -> comment = $row["comment"];
    }

            while ($row = $result2->fetch_assoc()) {
            //get the corresponding contact_id in each row,
            //this is the matching contact_id in the review_shared table of review_id

            $review -> checkedcontact = $row["contact_id"];
            }

                $json = json_encode($review);
            echo $json;

        ?>

1 个答案:

答案 0 :(得分:2)

您对象的价值会被覆盖。创建数组以获得多个值

class Review {

            public $category = "";
            public $category_id = "";
            public $name = "";
            public $phone = "";
            public $address = "";
            public $comment = "";

            public $checkedcontact = array(); // declare this as array

        }

然后在你的代码中

$review -> checkedcontact = array();
while ($row = $result2->fetch_assoc()) {
  //get the corresponding contact_id in each row,
  //this is the matching contact_id in the review_shared table of review_id

  $review -> checkedcontact[] = $row["contact_id"];
}