jakeoh Posted March 11, 2009 Share Posted March 11, 2009 I have an two-level array that contains, at the first level, a month of the year; the second level contains events held that month (name, description, start date, end date). I want to display this array in a table (events to come). The code I am using right now is this: <table class="calendrier"> <?php foreach($month_list as $month) { echo "<tr><td class='calendrier_mois' colspan='3'><h3>".$month['month_name']." </h3></td></tr>"; echo "<tr class='calendrier_head'>"; echo "<td class='calendrier_date'>Dates</td>"; echo "<td class='calendrier_even'>Événements</td>"; echo "<td class='calendrier_lieu'>Lieu</td>"; echo "</tr><tr>"; foreach($month['event_list'] as $event) { echo "<td class='calendrier_01'>".$event['event_startdate']."</td>"; echo "<td class='calendrier_02'>".$event['event_description']."</td>"; echo "<td class='calendrier_03'>".$event['event_location']."</td>"; echo "</tr>"; } } ?> </table> This is all fine, but I want to start with the current month; my array is ordered from January to December, so they are obviously ordered this way presently. How can I use PHP to start with the current month, and then cycle through the array and come back to January after December? Hope it's clear. Jake Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/ Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 have two loops. the first one starts at the index of the current month and goes to december. the second one starts at the beginning and goes up to but not including the current month Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-782316 Share on other sites More sharing options...
jakeoh Posted March 11, 2009 Author Share Posted March 11, 2009 have two loops. the first one starts at the index of the current month and goes to december. the second one starts at the beginning and goes up to but not including the current month I understand your technique. However I'm not sure how to code it. How can the foreach start on the current month? Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-782320 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 <?php $month_list = array(); for( $i=1;$i<13;$i++) { $month_list[$i] = date("F", mktime(0, 0, 0, $i, 1, 2008)); } print_r($month_list); $newmonthlist = $month_list; foreach($newmonthlist as $monthnumber=>$month) { if( $monthnumber < date("n")) { unset($newmonthlist[$monthnumber]); $newmonthlist[$monthnumber] = $month; } } echo "<br>"; print_r( $newmonthlist); ?> this should work for you... PS: we unsetting the months and setting the same values to them again in next line, but the difference here is it is an associative array and it will add elements at the bottom, so when u foreach they will come last... I have tried to imitate your month list, just fill it in with ur variable. Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-782328 Share on other sites More sharing options...
jakeoh Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks Samshel, it worked perfectly. Now just another thing... how can I append the year in my table, for every year (March 2009, April 2009, etc., January 2010, February 2010, etc.) Thanks a lot, again. Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-782937 Share on other sites More sharing options...
samshel Posted March 12, 2009 Share Posted March 12, 2009 <?php $month_list = array(); for( $i=1;$i<13;$i++) { $month_list[$i] = date("F", mktime(0, 0, 0, $i, 1, 2008)); } print_r($month_list); $newmonthlist = $month_list; foreach($newmonthlist as $monthnumber=>$month) { if( $monthnumber < date("n")) { unset($newmonthlist[$monthnumber]); $newmonthlist[$monthnumber] = $month." ".(date("Y")+1); } else { $newmonthlist[$monthnumber] = $month." ".date("Y"); } } echo "<br>"; print_r( $newmonthlist); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-783144 Share on other sites More sharing options...
jakeoh Posted March 13, 2009 Author Share Posted March 13, 2009 I made small adjustments so that it fits my code but all is well now. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/148991-solved-displaying-months-in-order/#findComment-783705 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.