Php 嵌套foreach问题
我有一个从mysql数据库打印约会的脚本,有两个foreach循环,一个打印日期,一个打印与该日期相关的约会。但是,第二个foreach循环只打印每个日期的第一项Php 嵌套foreach问题,php,mysql,loops,Php,Mysql,Loops,我有一个从mysql数据库打印约会的脚本,有两个foreach循环,一个打印日期,一个打印与该日期相关的约会。但是,第二个foreach循环只打印每个日期的第一项 <?php error_reporting(0); session_start(); require('sessions.inc.php'); Include('config.inc.php'); if(!isLoggedIn()) { echo("not logged in"); } else { //form
<?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>');
}
?>
您的mysql查询字符串中存在问题
SELECT id,title,date,time,place FROM tbl_diary GROUP BY date;
如果日期相同,此查询仅获取一行
试试这个:
SELECT id,title,date,time,place FROM tbl_diary;
实际上刚刚修复了这个问题,这是SQL查询中添加的按日期分组,无法解决如何删除这个问题…应该是问题下方的删除链接,除非n00bz:)@Andrew禁用了它,该代码可能需要一些清理。echo不是一个函数,include/require应该用作函数。耸肩谢谢,我已经清理好了,还有其他反馈吗?我的问题下还有链接编辑标志