Note that there are some explanatory texts on larger screens.

plurals
  1. PONested foreach issues
    primarykey
    data
    text
    <p>I have a script to print appointments from a mysql db, There is 2 foreach loops, one to print the date, and one to print the appointments related to that date. However the second foreach loop only prints the first item from each date. </p> <pre><code>&lt;?php error_reporting(0); session_start(); require('sessions.inc.php'); Include('config.inc.php'); if(!isLoggedIn()) { echo("not logged in"); } else { //form echo(' &lt;html&gt; &lt;center&gt;&lt;h1&gt;Add new Appontment&lt;/h1&gt;&lt;/center&gt; &lt;form name="login" action="diary.php" method="post"&gt; Date (YYYY-MM-DD): &lt;input type="text" name="date"/&gt;&lt;br&gt; Time (HH-MM-SS):&lt;input type="text" name="time"/&gt;&lt;br&gt; Description:&lt;input type="descrip" name="descrip"/&gt;&lt;br&gt; Place:&lt;input type="place" name="place"/&gt;&lt;br&gt; &lt;input type="submit" value="sumbit"/&gt; &lt;/form&gt; &lt;/html&gt; '); //Connect to DB $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); //Get Data from DB $query = "SELECT id,title,date,time,place FROM tbl_diary GROUP BY date;"; $result = mysql_query($query) or die(mysql_error()); //Get Data from form $date = mysql_real_escape_string($_POST['date']); $time = mysql_real_escape_string($_POST['time']); $descrip = mysql_real_escape_string($_POST['descrip']); $place = mysql_real_escape_string($_POST['place']); //insert into DB if (isset($_POST['date'],$_POST['time'],$_POST['descrip'],$_POST['place'])) { $insert = "INSERT INTO tbl_diary (date,time,title,place) VALUES ('$date','$time','$descrip','$place')"; if (!mysql_query($insert)) { die('Error: ' . mysql_error()); } echo ('New appontment at'.$_POST['place'].'added, On'.$_POST['date'].".&lt;br&gt;"); echo ('&lt;a href="./index.php?page=diary"&gt;Please click to reload page&lt;/a&gt;'); } //Intizlize array $array = array(); //Display appontments while($input = mysql_fetch_array($result)) { if (!isset($array[$input['date']])) { $array[$input['date']] = array(); } //put to end of array $array[$input['date']] []= $input; } //The first for each prints each date, The second prints the times. foreach ($array as $TheDate =&gt; $items) { $therealdate = date("d-m-Y", strtotime($TheDate)); //welcome to europe MySql echo '&lt;h1&gt;'.$therealdate.'&lt;/h1&gt;'; echo "&lt;ul&gt;"; foreach ($items as $item) { echo "&lt;li&gt;{$item['time']}: {$item['title']} "."@ {$item['place']}&lt;/li&gt;"; } echo "&lt;/ul&gt;"; } //exit echo ('&lt;a href="./main.php"&gt;Return to main&lt;/a&gt;'); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload