我有一个脚本来打印来自mysql数据库的约会,有2个foreach循环,一个用于打印日期,另一个用于打印与该日期相关的约会。但是,第二个foreach循环仅打印每个日期的第一个项目。
<?php
error_reporting(0);
session_start();
require('sessions.inc.php');
Include('config.inc.php');
if(!isLoggedIn())
{
echo("not logged in");
}
else
{
//form
echo(' <html>
<center><h1>Add new Appontment</h1></center>
<form name="login" action="diary.php" method="post">
Date (YYYY-MM-DD): <input type="text" name="date"/><br>
Time (HH-MM-SS):<input type="text" name="time"/><br>
Description:<input type="descrip" name="descrip"/><br>
Place:<input type="place" name="place"/><br>
<input type="submit" value="sumbit"/>
</form>
</html>
');
//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'].".<br>");
echo ('<a href="./index.php?page=diary">Please click to reload page</a>');
}
//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 => $items)
{
$therealdate = date("d-m-Y", strtotime($TheDate));
//welcome to europe MySql
echo '<h1>'.$therealdate.'</h1>';
echo "<ul>";
foreach ($items as $item)
{
echo "<li>{$item['time']}: {$item['title']} "."@ {$item['place']}</li>";
}
echo "</ul>";
}
//exit
echo ('<a href="./main.php">Return to main</a>');
}
?>
答案 0 :(得分:1)
你的mysql查询字符串有问题。
SELECT id,title,date,time,place FROM tbl_diary GROUP BY date;
如果日期相同,此查询只会获取一行。
试试这个:
SELECT id,title,date,time,place FROM tbl_diary;