Fixtures in groups of timestamp
The following code brings up all the fixtures from the database.
while ($row = mysql_fetch_assoc($result))
{
echo $row['date'];
echo $row['home_user'];
echo $row['home_team'];
echo $row['away_user'];
echo $row['away_team'];
}
The problem I have is all the fixtures are just listed. There are many fixtures with the same dates. For each date(timestamp), there are 10 fixtures. What I am trying to achieve is each date printed and then the fixtures for that da开发者_如何学运维te underneath. Is this possible? Thanks
This is a common question for users of SQL databases. It's natural to report information in this kind of way, however relational query results are always "square" -- that is, every row has the same columns, and every column must exist on every row. So the shape of a query result doesn't fit how you want to display it.
The easiest way to do what you want is to include the date on every row of the query result, and then echo it conditionally, only when it changes:
$prevDate = '';
while ($row = mysql_fetch_assoc($result))
{
if ($row['date'] != $prevDate) {
echo 'DATE: ' . $row['date'];
$prevDate = $row['date'];
}
echo $row['home_user'];
echo $row['home_team'];
echo $row['away_user'];
echo $row['away_team'];
}
Be sure to write your SQL query to ORDER BY date
.
精彩评论