Hi all! Not sure this belongs here but I think it does.
Ok so I have a database to store the results and dates of matches. The database is like this:
HTeam HScore ATeam AScore Date
I can display my data by date like this so far:
07-08-2008 Team A 12 - 3 Team B
07-07-2008 Team C 12 - 3 Team D
07-06-2008 Team E 12 - 3 Team F
07-07-2007 Team G 12 - 3 Team H
06-07-2006 Team I 12 - 3 Team J
07-07-2005 Team K 12 - 3 Team L
07-07-2004 Team M 12 - 3 Team N
As you can see my dates are already in DESC order, but I want to have some code that will display the matches in groups by their date. For example if 3 matches were played on 02-03-2005 and 2 matchs were displayed on 22-07-2008 it would display like this:
22-07-2008
Team G 12 - 3 Team H
Team I 12 - 3 Team J
02-03-2005
Team A 12 - 3 Team B
Team C 12 - 3 Team D
Team E 12 - 3 Team F
Any help would be much appreciated as this is driving me crazy!!
Display data by date
Moderator: General Moderators
Re: Display data by date
You currently just have it print out each row at a time. To achieve something of the nature in which you speak, I would do the following (code not tested, just to give you an idea):
Hope that helps.
Code: Select all
$result = getStuffFromDatabase();
$last_date;
while($row = mysql_fetch_array($result)){
if($row['date']) != $last_date)
echo $row['date']."<br>";
echo $row['HTeam']." ".$row['HScore']." - ".$row['ATeam']." ".$row['AScore']."<br>";
$last_date = $row['date'];
}
-
Darragh3277
- Forum Newbie
- Posts: 12
- Joined: Tue Jun 24, 2008 9:19 am
Re: Display data by date
Hey thanks for the quick reply. It's almost right. This is the code I put in
and the output im getting is this:
07-08-2008
TeamA 12 - TeamB 3
07-08-2008
TeamC 23 - TeamD 7
07-07-2008
TeamE 12 - TeamF 3
07-06-2008
TeamG 12 - TeamH 3
As you can see the top two results have the same date, I was hoping for something more like this:
07-08-2008
TeamA 12 - TeamB 3
TeamC 23 - TeamD 7
07-07-2008
TeamE 12 - TeamF 3
07-06-2008
TeamG 12 - TeamH 3
Where the teams with the same date are grouped together. Any ideas?
Code: Select all
while($row = mysql_fetch_array($result)){
$dateTime = new DateTime($row['Date']);
$NewDate = date_format($dateTime, "d-m-Y");
if($row['Date'] != $last_date)
echo $NewDate."<br>";
echo $row['HTeam']." ".$row['HScore']." - ".$row['ATeam']." ".$row['AScore']."<br><br>";
$last_date = $row['date'];
}07-08-2008
TeamA 12 - TeamB 3
07-08-2008
TeamC 23 - TeamD 7
07-07-2008
TeamE 12 - TeamF 3
07-06-2008
TeamG 12 - TeamH 3
As you can see the top two results have the same date, I was hoping for something more like this:
07-08-2008
TeamA 12 - TeamB 3
TeamC 23 - TeamD 7
07-07-2008
TeamE 12 - TeamF 3
07-06-2008
TeamG 12 - TeamH 3
Where the teams with the same date are grouped together. Any ideas?
Re: Display data by date
Code: Select all
while($row = mysql_fetch_array($result)){
$dateTime = new DateTime($row['Date']);
$NewDate = date_format($dateTime, "d-m-Y");
if($row['date'] != $last_date) {
echo $NewDate."<br>";
}
echo $row['HTeam']." ".$row['HScore']." - ".$row['ATeam']." ".$row['AScore']."<br><br>";
$last_date = $row['date'];
}
Re: Display data by date
I believe the code is right, there is just a discrepancy in your $row['date'] or $row['Date']. Not sure which one you meant to use, but you are using both.
mabwi's code still has this discrepancy as well. So make sure you go through and check those.Darragh3277 wrote:Hey thanks for the quick reply. It's almost right. This is the code I put inCode: Select all
$dateTime = new DateTime($row['Date']);Code: Select all
$last_date = $row['date'];
-
Darragh3277
- Forum Newbie
- Posts: 12
- Joined: Tue Jun 24, 2008 9:19 am
Re: Display data by date
You were right. That worked perfectly, thanks so much my head was killing me trying to figure the blasted thing out.
Cheers
Cheers