简体   繁体   中英

How do I make a JSON object using content from these two tables in my database?

This is the format I want :

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

This is what I am getting :

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

I have no problem with the JSON output from the review table, it is the review_shared table that is causing me problems. For checkedcontact , how do I get an array? Because right now only one value of checkedcontact comes up in my JSON object, even though I might have several values in the table.

Here's my code:

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

        ?>

Your value for object is overwriting. Create array to get multiple value

class Review {

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

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

        }

Then in your code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM