Jump to content

simple looping problem


unistake
Go to solution Solved by Barand,

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
Share on other sites

  • Solution

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.