Hope someone can solve this.
I have the following MySQL query:
$sql = $db->prepare("SELECT
dienaren.id,
dienaren.achternaam_minister,
dienaren.email_minister,
dienaren.gemeente,
users.achternaam,
users.email,
users.periode_van,
users.periode_tot
FROM dienaren
LEFT JOIN users
ON dienaren.id = users.minister_id
WHERE users.periode_tot < DATE_ADD(CURDATE(), INTERVAL + 31 DAY)
ORDER BY dienaren.id,users.periode_tot ASC");
$sql->execute();
This is giving me the result of 29 rows (which is correct as I checked)
Now I want to use a kind of loop to show a table where person A has x rows that correspond with the query. The same counts for person B, person C etc.
Here is screenshot with the current output:
As you can see there are 2 records that are true for this person. But it is showing two lists instead of one total list.
Here is my code that produces this table:
<?php
$count = $sql->rowCount();
echo '<p>Total result SQL query: '.$count.'</p>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php foreach($sql->fetchAll(PDO::FETCH_OBJ) as $voorganger): ?>
<div><strong>Person ID: <?= $voorganger->id;?></strong></div>
<div>Is in control of the following users:</div>
<table>
<tr>
<th>Voorletter(s)</th>
<th>Periode Tot</th>
<th>Status</th>
</tr>
<?php
$query = $db->prepare("SELECT * FROM users WHERE minister_id = '{$voorganger->id}' AND periode_tot < DATE_ADD(CURDATE(), INTERVAL + 31 DAY) ORDER BY minister_id ASC");
$query->execute();
foreach($query->fetchAll(PDO::FETCH_OBJ) as $user) : ?>
<?php $date = new DateTime($user->periode_tot); ?>
<tr style="background:rgba(244, 65, 55, 0.35);">
<td><?= $user->voorletters; ?></td>
<td><?= $date->format('d-m-Y'); ?></td>
<?php if (dateDifference($user->periode_tot,$currentdate) > 0 && dateDifference($user->periode_tot,$currentdate) <= 30) { ?>
<td>Verloopt over <?= dateDifference($user->periode_tot,$currentdate); ?> dagen</td>
<?php } else { ?>
<td>Verlopen</td>
<?php } ?>
</tr>
<?php endforeach; ?>
</table>
<hr>
<?php endforeach; ?>
</body>
</html>
Hope this is clear to the one to wants to help me. If not just let me know to explain more. (Btw: I changed some outcome in the table because it holds actual data which it's private)
Thanks in advance.
EDIT:
Just pointing out that in the database I have a dienaren table that has many relationship on T2 (holding users with a field called minister_id)
Looping through the users table having eg. 5 results there is a table created 5 times with all the 5 results (zie screenshot 2)
. What I want instead is just ONE table that holds those 5 results. I think I am not using the right loop function or I am not using it right anyway.
SECOND EDIT:
I took a different approach which got me near to my solution. I changed from a JOIN query to SELECT * FROM T1. Only downside is that it also show all the records that are having 0 results. So how can I prefend the creation of a table from the person from T1 that has no results from the query in T2 ?
(In this case ID:861 holds 5 records, but ID:862 has no records but is also has a table which is empty. I want to get rid of tables that are empty)

