Mysql group from php concat

I have a table of fixtures in my database: hometeam, awayteam, date, time

I am currently using mysql_fetch_arrayand getting the following:

hometeam    awayteam    date         time
--------------------------------------------
Blackburn   Wolves      13/08/2011   3:00pm
Arsenal     Liverpool   13/08/2011   3:00pm
etc

I want to return:

13/08/2011
---------------------------
Blackburn, Wolves, 3:00pm
Arsenal, Liverpool, 3:00pm
etc

I tried a group by date, but that just returns 1 binding for that date, which is garbage.

I also tried group_concat, but the problem with this is that I need to change the elements in this group, such as changing the time format from server time to read time, as mentioned above.

0
source share
1 answer

you can sort by date and then check php when the date changes between two lines. something like that:

function h($s) { return htmlspecialchars($s); }
$conn = mysqli_connect(…);
$result = mysqli_query($conn, 'select * from fixtures order by date');

$lastdate = NULL;
while(($row = mysqli_fetch_assoc($result)) !== FALSE) {
  if($lastdate != $row['date']) {
    // output date divider/header
    echo '<h1>',h($row['date']),'</h1>';
    $lastdate = $row['date'];
  }

  echo h($row['hometeam']), ', ', h($row['awayteam']), ', ', h($row['time']);
}
0
source

All Articles