I want to save Databaseentrise with the key "weekday, date" in an array with an array. (Idea is that I could save xx guests on weekday xxx in an array. And to output a lists of guests on weekday xxx or in a Lists of Days Mo-Sun.)
After that I want to output the array. The array should be two entries but in the output it shows me only the last entrie. What do I wrong?
My MySQL DB is this: guests:
Id | firstname | lastname | begindate | endate |
---|---|---|---|---|
1 | Otto | Testm | 2022-06-30 | 2022-06-30 |
2 | Anna | Testw | 2022-06-30 | 2022-06-30 |
3 | Peter | Testma | 2022-06-27 | 2022-06-29 |
<?php
//DB: Connection
$mysqli = new mysqli("localhost", "root", "", "Belegung");
//Errorhandling
if ($mysqli -> connect_errno)
{
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
//Prepare Array
$calendar = array ();
//DB: SQL:
$sql = "SELECT * FROM `guests` WHERE `begindate` = '2022-06-30'";
$result = $mysqli->query($sql) or die( $result->error);
//Fillign Array Key: *Monday, YYYY-MM-DD* - *Sunday, YYYY-MM-DD* and as Array name of guests
while ($row = $result->fetch_object())
{
$calendar['2022-06-30'] = array ($row->firstname . " " . $row->lastname);
//With print_r($row->firstname) I get here two names, so it should work but:
}
//Output
$tag ='2022-06-30';
echo "<ul>";
foreach ($calendar[$tag] as $ausgabecal)
{
echo "<li>" . $ausgabecal . "</li>" ."<br/>";
//Here I got only the last one "Anna
}
echo "</ul>";
$result -> free_result();
$mysqli -> close();
?>