Display data by date

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Display data by date

Post by Darragh3277 »

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!!
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: Display data by date

Post by Dynamis »

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):

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'];
}
 
 
Hope that helps.
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Re: Display data by date

Post by Darragh3277 »

Hey thanks for the quick reply. It's almost right. This is the code I put in

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'];
}
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?
mabwi
Forum Commoner
Posts: 27
Joined: Wed Aug 01, 2007 4:51 pm

Re: Display data by date

Post by mabwi »

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'];
}
 
You had a typo in your code, in the if statement.. It's also a good idea to include the curly braces around the conditional statement.
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: Display data by date

Post by Dynamis »

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.
Darragh3277 wrote:Hey thanks for the quick reply. It's almost right. This is the code I put in

Code: Select all

$dateTime = new DateTime($row['Date']);

Code: Select all

&nbsp; $last_date = $row['date'];
mabwi's code still has this discrepancy as well. So make sure you go through and check those.
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Re: Display data by date

Post by Darragh3277 »

You were right. That worked perfectly, thanks so much my head was killing me trying to figure the blasted thing out.

Cheers
Post Reply