我现在有点卡住了,希望你们给我打电话帮助我。
我的数据库中有一个表格如下:
Game - kickoff - result
g1 - 2014-12-23 15:00:00 - 1
g2 - 2014-12-23 15:00:00 - 1
g3 - 2014-12-24 15:00:00 - 1
g4 - 2014-12-24 15:00:00 - 1
g5 - 2014-12-25 15:00:00 - 1
我想在我的页面中显示它,所以它看起来像这样:
23-12-2014:
G1 - 15:00
G2 - 15:00
24-12-2014
G3 - 15:00
G4 - 15:00
25-12-2014
G5 - 15:00
所以按日期安排,我可以弄清楚我应该怎么做。
这就是我的地方,但我不知道如何按日期分开。
$tips = $db -> select("SELECT * FROM games");
foreach($tips as $result) {
$game= $result['game'];
$kickoff= $result['kickoff'];
$result_end= $result['result'];
echo $game . $kickoff . $result_end;
}
答案 0 :(得分:1)
您可以单独设置日期和时间的格式,以便也可以单独显示它们。 然后,您可以保留一个变量来检查日期是否与上一个日期不同。
对查询进行排序也很重要(添加ORDER BY),否则同一日期可能会多次出现。
此代码假定kickoff
是时间戳。如果它是一个字符串,您可能需要以另一种方式解析它,但这是一个特定的实现细节。
<?php
$tips = $db -> select("SELECT * FROM games ORDER BY kickoff");
$lastkickoffdate = '';
foreach($tips as $result) {
$game= $result['game'];
$kickoff= $result['kickoff'];
$result_end= $result['result'];
$kickoffdate = date("Y-m-d", $kickoff);
$kickofftime = date('H:i', $kickoff);
if ($kickoffdate !== $lastkickoffdate)
{
echo $kickoffdate . '<br>';
}
echo "$game - $kickofftime - $result_end";
}
答案 1 :(得分:0)
我认为你需要改变
$tips = $db -> select("SELECT * FROM games");
到
$tips = $db -> select("SELECT * FROM games ORDER BY kickoff");
答案 2 :(得分:0)
试试此代码
$tips = $db->select("SELECT * FROM games ORDER BY kickoff");
$lastkickoffdate = '';
foreach($tips as $result) {
$game = $result['game'];
$kickoff = $result['kickoff'];
$kickoffdate = date("d-m-Y", strtotime($kickoff));
$kickofftime = date('H:i', strtotime($kickoff));
$result_end = $result['result'];
if($kickoffdate != $lastkickoffdate){
echo '<p>'.$kickoffdate.'</p>';
}
echo ucfirst($game) .' - '. $kickofftime.'<br>';
$lastkickoffdate = $kickoffdate;
}