I am relatively new to PHP and MYSQL and have been trying to write a program that will connect to a database and GET information from it to display on a webpage.
So far I have 2 tables:
orderID
table
and
reviews
table.
In the orderID
table the id
column is my primary key and is set to AUTO-INCREMENT, whilst the oid
is set as a foreign key in the reviews
table.
I want to be able to display the oid
, and then also the name
and comments
for each oid
, in JSON format on my webpage, like so:
[
{
"objectID": "ABC1234X",
"comments": [
{
"name": "Mary",
"comment": "This is a comment"
},
{
"name": "Greg",
"comment": "Another comment"
}
]
},
{
"objectID": "DEF1234X",
"comments": [
{
"name: "Greg",
"comment: "A comment"
},
{
"name": "Ben",
"comment": "Oh look, a comment"
}
]
}
]
So far I have been able to do this for only the oid
with a value of ABC1234X
(corresponding to id
value 1, in the orderID
table), using the following code (my username, password, and dbname are all different in my actual code.):
<?php
$dbHostname = "localhost";
$username = "XXXXXXXXXX";
$dbPassword = "XXXXXXXXXX";
$dbName = "XXXXXXXXXXXXXXXXX";
$conn = new mysqli($dbHostname, $username, $dbPassword, $dbName);
if($conn->connect_errno > 0) {
die('Unable to connect to database [' . $conn->connect_error . ']');
}
$oidList_array = array();
$objectID_array = array();
$review_array = array();
$id = 1;
$objectQuery = "SELECT * FROM orderID;";
$reviewQuery = "SELECT * FROM reviews WHERE oid IN (SELECT oid FROM orderID WHERE id=$id);";
$oidResult = mysqli_query($conn, $objectQuery);
$reviewResult = mysqli_query($conn, $reviewQuery);
$resultCheck = mysqli_num_rows($oidResult);
if($resultCheck < 0) {
die('There was an error running the query [' . mysqli_error($conn) . ']');
}
else if ($resultCheck > 0) {
while ($row_oid = mysqli_fetch_assoc($oidResult)) {
$objectID_array['objectID'] = $row_oid['oid'];
$objectID_array['comments'] = array();
while ($row_review = mysqli_fetch_assoc($reviewResult)) {
$review_array['name'] = $row_review['name'];
$review_array['comment'] = $row_review['comment'];
array_push($objectID_array['comments'], $review_array);
}
array_push($oidList_array, $objectID_array);
}
header('Content-Type: application/json');
$jsonData = json_encode($oidList_array, JSON_PRETTY_PRINT);
echo "<pre>" . $jsonData . "<pre>";
}
This currently gives me this JSON output:
[
{
"objectID": "ABC1234X",
"comments": [
{
"name": "Mary",
"comment": "This is a comment"
},
{
"name": "Greg",
"comment": "Another comment"
}
]
},
{
"objectID": "DEF1234X",
"comments": []
}
]
How do I go about incrementing the $id
value and then rerunning the if
and while
statements, so that the name
and comment
appears for "objectID": "DEF1234X"
?
Is it as simple as just adding $id++
somewhere into the code? If so where would that go?
I thought about adding a for
loop around the if
and while
statements, but was unsure if that would work and also what the conditions would be. If possible I would like to leave room to add more data in the future, so something like for ($id = 1; $id <= 2; $id++) {}
wouldn't be viable (if it would even work in the first place).
Thank you in advance for any help that can be given.