kristen Posted December 10, 2015 Share Posted December 10, 2015 I imagine this is an easy one for you all! I actually asked a very similar question probably 10 years ago, doesn't seem to be a way to find it in my history though :/ Thanks in advance for helping out a very, very casual coder. I have a query that pulls events from my table. Events use `date` field if it is on a specific date, or the `date_from` and `date_to` fields if they occur over a range of dates. SELECT * FROM cms_events WHERE county_id='7' AND status='1' and ((`date_to`>='2015-12-10' || `date`>='2015-12-10')) ORDER BY date_from,date,name ASC Returned on the page, I would like to have a header separating events by date. So it would print "December 10", list results for 2015-12-10, then "December 11", list results for 2015-12-11, etc. Then at the end, "Ongoing" events, which are the ones using date_from and date_to fields. Thank you for your help! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 10, 2015 Share Posted December 10, 2015 if would be a little conditional logic - // example data $data[] = array('date'=>'2015-12-10','date_from'=>'','date_to'=>'','name'=>'event1'); $data[] = array('date'=>'2015-12-11','date_from'=>'','date_to'=>'','name'=>'event2'); $data[] = array('date'=>'2015-12-11','date_from'=>'','date_to'=>'','name'=>'event3'); $data[] = array('date'=>'','date_from'=>'2015-12-09','date_to'=>'2015-12-10','name'=>'event4'); $data[] = array('date'=>'','date_from'=>'2015-12-20','date_to'=>'2015-12-29','name'=>'event5'); $last_value = null; // use to detect date change for multiple events on a date $first_pass = true; // used to output a one time label foreach($data as $row){ // loop over the data. you can either fetch all your rows into an array named $data, or replace this line of code with a while() loop that loops over the result set from your query if($row['date']){ // there is a specific date, do the specific date handling // if the date changes or is the first one, output a new heading if($last_value != $row['date']){ // save the new date as the last value $last_value = $row['date']; // format the date for display $date = date_create($row['date']); echo date_format($date, 'F j') . '<br>'; } // output the data under the heading echo $row['name'] . '<br>'; } else { // there is not a specific date, do the general date handling // output a one time label for this section if($first_pass){ echo "Ongoing Events -<br>"; $first_pass = false; } // format the dates for display $date1 = date_create($row['date_from']); $date2 = date_create($row['date_to']); echo date_format($date1, 'F j') . ' to ' . date_format($date2, 'F j') . ' ' . $row['name'] . '<br>'; } } the ORDER BY term you have in your query should produce the correct output. you would want the rows, having a date, first in the result set. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.