如何更改下面的代码,以便我只回显每个独特的“$ course”一次?
例如。目前我的结果看起来像=
伊普斯威奇-11:00运行的 ipswich-12:00-火烈鸟骑手 伊普斯威奇-14:00雷击 norwich-13:10-ed又回来了 诺维奇-14:05-REDRUM 诺维奇-17:05-泡菜
但我希望它们看起来像=
伊普斯威奇
11:00运行的 12:00-火烈鸟骑手 14:00雷击
诺维奇
13:10-ed回来了 14:05-REDRUM 17:05-泡菜我想过为每个循环做一个mysqli查询,但肯定有更好的方法吗?
我的代码=
<?php //connection block
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT horse, course, time, date FROM dailytips WHERE date = $today ORDER BY course, time";
$result = $mysqli->query($query);
$today_uk = " " . date("d/m/y");
while($row = $result->fetch_array())
{ $rows[] = $row; }
echo "<h2>tips for" .$today_uk. "</h2>";
foreach($rows as $row)
{
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$horse = $row['horse'];
$time = $row['time'];
$course = $row['course'];
echo
'<div style= "width:600px; font-family:verdana;">
<div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">
'.$row['course']. "-" .$row['time'] . "-" . $row['horse'] .'
</div>' ;
}
$result->close();
$mysqli->close();
?>
答案 0 :(得分:0)
我猜你可以将一个foreeach语句嵌入另一个语句中。
所以...
$query = "SELECT course, FROM dailytips WHERE date = $today ORDER BY course, time";
while($row = $result->fetch_array())
{ $rows[] = $row; }
echo "<h2>tips for" .$today_uk. "</h2>";
foreach($rows as $row) {
$query = "SELECT horse, time, date FROM dailytips WHERE course = $row['course'];
while($row = $result->fetch_array())
{ $rowDetail[] = $rowDetails; }
foreach($rows as $row) {
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$horse = $row['horse'];
$time = $row['time'];
$course = $row['course'];
echo
'<div style= "width:600px; font-family:verdana;">
<div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">
'.$row['course']. "-" .$row['time'] . "-" . $row['horse'] .'
</div>' ;
}
}
我会考虑将你的结果放在一个与DIV相对的表中,因为这似乎符合你要做的更好。在循环之前设置表格,并在退出循环后关闭。
希望这有帮助。
取值