My problem is:
I am joining tables together, and some tables have multiple data which I should fetch, but my JSON object only retrieves one. I think the reason for this is because the while loop loops through my main table aswell, fetches everything and stops looping, considering there is nothing left to loop through (I fetch my data based on my id in my database and these are auto_incremented etc).
So my question would be:
How can I loop through my main table (once), but keep on looping through the joined tables and insert these values in their 'own' array?
this is what I have right now:
$sqli = "SELECT event.*, typex.id, typex.type
FROM event
JOIN typex ON event.id = typex.id
WHERE event.id = ?";
mysqli_stmt_bind_param($stmt, 's', $editID); //fetch data based on ID
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data['all'] = array($row);
$data['type'] = array($row['type']);
}
//Echo data as JSON
echo json_encode($data);
So to give you an idea:
$data['all'] is the array which will hold all $row (this is so I can easily access columns from my main table)
$data['type'] however should only hold data from the table: typex (typex is a joined table).
an example:
You have a main table called: stack and a table you joined called: typex.
You want to insert all the rows with id number 5 from the table stack in the array $data['all'], considering the main table has an auto_increment on the id and unique, it will loop once... but now comes the part I fail at and need your help:
In your joined table: typex you have 2 rows with id number 5. these 2 rows should be fetched and inserted in the array $data['type']. How could this be achieved?