Jump to content

simple looping problem


unistake

Recommended Posts

Hi guys, 

 

I am trying to echo multiple calendar events from a mysql table and order them by the date column.

There are several events on each day so I am trying to echo the date once then list the associated events under that date.

 

Such as 

2014-05-10 

Event 1, event 2, event 3

2014-05-11

Event 1, event 2, event 3

 

instead of 

2014-05-10 event 1,

2014-05-10 event 2,

2014-05-10 event 3, 

2014-05-11 event 1 etc...

 

This is my coding so far:

while($row = mysqli_fetch_assoc($result)) {
	$dutydate = $row['MyDate'];

	if($dutydate != $previousdate) {
		echo $dutydate.'<br />';	
	}
	elseif(!empty($dutydate)) {
		echo $dutydate.'<br />';	
	}

        echo $event1.' | '.$event2.' | '.$event3.'<br />';
        $previousdate = $dutydate;
}		
?>
Link to comment
https://forums.phpfreaks.com/topic/288555-simple-looping-problem/
Share on other sites

try

$previousdate='';
$events = array();
while($row = mysqli_fetch_assoc($result)) {
    $dutydate = $row['myDate'];

    if($dutydate != $previousdate) {
        if ($previousdate)  {
            echo $previousdate . '<br>' . join(", ", $events) . '<br><br>';
        }
        $events = array();
        $previousdate = $dutydate;  
    }
    $events[] = $row['event_name'];    
}
echo $previousdate . '<br>' . join(", ", $events) . '<br><br>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.