Hi I have the following table that is generated from my database, it looks like this
The problem is I dont want the dates to repeat. If there is more than one fixture for a specific date, I only want the date displayed once and then the fixtures under the date.
Example In this case, on the image, Today,2015-04-23 must be displayed only once and then the 2 team names under it.
Im using the following code
$echoed = false;
while($row = > $played){
$gameDate = $row['event_date'];
$team1 = $row['team1'];
$team2 = $row['team2'];
$venue = $row['venue'];
$eventId = $row['event_id'];
if($gameDate == $date && !$echoed){
echo'<tr>';
echo $echoed ='<td>Today,'.$gameDate.'echo</td>';
echo'</tr>';
echo'<tr>';
echo'<td>'.$team1.'</td>';
echo'<td>'.$team2.'</td>';
echo'<td>'.$venue.'</td>';
:
:
However code above is not giving desired result and returns result same as per image above
You can check, if the date is the same as the one before:
<?php
$lastDate = '';
while($row = > $played){
$gameDate = $row['event_date'];
$team1 = $row['team1'];
$team2 = $row['team2'];
$venue = $row['venue'];
$eventId = $row['event_id'];
if($gameDate !== $lastDate) {
echo'<tr>';
$lastDate = $gamedate;
echo $echoed ='<td>Today,'.$gameDate.'echo</td>';
echo'</tr>';
}
echo'<tr>';
echo'<td>'.$team1.'</td>';
echo'<td>'.$team2.'</td>';
echo'<td>'.$venue.'</td>';
Note: i removed your echoed code and your if-statement, because $date is undefined :)
Save the last date in each loop and just check with date in current step.
$last_date = '';
while (...) {
$gameDate = $row['event_date'];
// set other variables
if ($gameDate != $last_date) {
// write $gameDate here
}
// teams table here
$last_date = $gameDate;
}
Instead of using $echoed
the way you do, you can use the $date
variable only to keep track of the current date you are printing for. Then you only print the date itself when it changes.
Something like:
// I am using $date as it seems undefined, but if you already use that, name it something else
$date = false;
while($row = > $played){
$gameDate = $row['event_date'];
$team1 = $row['team1'];
$team2 = $row['team2'];
$venue = $row['venue'];
$eventId = $row['event_id'];
if($gameDate !== $date){
$date = $gameDate;
echo'<tr>';
echo $echoed ='<td>Today,'.$gameDate.'echo</td>';
echo'</tr>';
}
echo'<tr>';
echo'<td>'.$team1.'</td>';
echo'<td>'.$team2.'</td>';
echo'<td>'.$venue.'</td>';
...