-2

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();
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Because `$calendar['2022-06-30'] = ...` overwrites the entry for that key on every iteration. Use `$calendar['2022-06-30'][] = ...` or [array_​push](https://www.php.net/manual/en/function.array-push.php). Also, no need to use `array ($row->firstname . " " . $row->lastname);`, a simple `$row->firstname . " " . $row->lastname;` would do – brombeer Jun 30 '22 at 07:44
  • It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jun 30 '22 at 07:47
  • Thank you three for you hint. It helps me a lot! :-) And thank you for the security @Dharman: I will look at you post. – maximiliansport Jun 30 '22 at 08:09

1 Answers1

0

you should use this in your loop

while ($row = $result->fetch_object())
{
   $calendar['2022-06-30'][] = array ($row->firstname . " " . $row->lastname);
}
Ali SSN
  • 357
  • 3
  • 6
  • Nice! Thank you very mutch! Yes, now I see also my misstake in brain :-D Now the passage looks and works like this with previous code of output: `while ($row = $result->fetch_object()) { $calendar['2022-06-30'][] = $row->firstname . " " . $row->lastname; }` – maximiliansport Jun 30 '22 at 08:07
  • that's good, but better is use dynamic date key: $calendar[$row->begindate][] = array ($row->firstname . " " . $row->lastname); } – Ali SSN Jun 30 '22 at 13:49