samoht Posted July 9, 2007 Share Posted July 9, 2007 hello all, I have an event calendar that is quite clunky for a business that often only has 1 or 2 events in a month and would like to view all upcoming events (or at least with in the next 6mo. to a year) so I would like to change from displaying a traditional calendar by the month showing every day - to a two column list of the next 6 to 12 months that becomes a link if there are any events within the month ( and probably display the number of events next to the month...e.g. August (3) etc.) Any thought on how I would construct the php loops for the months? Thanks for any help Andy Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/ Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 Use modulos (%) <?php $currentMonth = 4; $count = 12; $display = ""; while (true) { if (($counter % 2) == 0) { $display .= "<td>" . $currentMonth . "</td></tr>"; }else { $display .= "<tr><td>" . $currentMonth . "</td>"; } if ($currentMonth == 12) { $currentMonth = 0; } if ($counter == 12) { break; // kill the loop } $currentMonth++; $counter++; } echo "<table>" . $display . "</table>"; ?> Something like that? Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293475 Share on other sites More sharing options...
samoht Posted July 9, 2007 Author Share Posted July 9, 2007 Thanks for the start, quick question - If I wanted to display the months as 07, instead of 7 etc how would I change the code? + is there an easy way to retrieve the month name inside the loop? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293491 Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 Thanks for the start, quick question - If I wanted to display the months as 07, instead of 7 etc how would I change the code? + is there an easy way to retrieve the month name inside the loop? Thanks Put the months into an array. <?php $months = array(1=> array("num" => "01", "name" => "January"), 2 => array("num" => "02", "name" => "February")); // ..etc $currentMonth = 4; $count = 12; $display = ""; while (true) { if (($counter % 2) == 0) { $display .= "<td>" . $months[$currentMonth]['name'] . "</td></tr>"; }else { $display .= "<tr><td>" . $currentMonth[$currentMonth]['name'] . "</td>"; } if ($currentMonth == 12) { $currentMonth = 0; } if ($counter == 12) { break; // kill the loop } $currentMonth++; $counter++; } echo "<table>" . $display . "</table>"; ?> Change 'name' to 'num' to get the number. Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293501 Share on other sites More sharing options...
samoht Posted July 9, 2007 Author Share Posted July 9, 2007 hmm, This gives me a Fatal error: Cannot use string offset as an array ? Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293506 Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 <?php $months = array(1=> array("num" => "01", "name" => "January"), 2 => array("num" => "02", "name" => "February")); // ..etc $currentMonth = 4; $count = 12; $display = ""; while (true) { if (($counter % 2) == 0) { $display .= "<td>" . $months[$currentMonth]['name'] . "</td></tr>"; }else { $display .= "<tr><td>" . $months[$currentMonth]['name'] . "</td>"; // error was here } if ($currentMonth == 12) { $currentMonth = 0; } if ($counter == 12) { break; // kill the loop } $currentMonth++; $counter++; } echo "<table>" . $display . "</table>"; ?> my bad. Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293515 Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 You can get the next 12 months as numbers and text without using arrays, by using the date() and strototime() functions. Might save some typing : <?php $curr_month= 7; for($x=0;$x<12;$x++){ $month_num =($x+$curr_month > 12) ? $x+$curr_month -12 : $x+$curr_month; echo date('m',strtotime($month_num.'/01')); echo ' - '.date('F',strtotime($month_num.'/01')).'<br />'; } ?> Produces: 07 - July 08 - August 09 - September 10 - October 11 - November 12 - December 01 - January 02 - February 03 - March 04 - April 05 - May 06 - June Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293523 Share on other sites More sharing options...
samoht Posted July 9, 2007 Author Share Posted July 9, 2007 Thanks for the help! The less code the better! Anyway, one last element I need is the total number of events to display with the month I have a query of two tables: $query_rsEvents = "SELECT events.EventId, events.EventLocationId, events.Name, events.ShortDescr, events.Descr, events.ImageURL, events.PDF, eventdates.EventDate, eventdates.Note, eventdates.TimeStart, eventdates.TimeEnd FROM events, eventdates WHERE eventdates.EventId = events.EventId AND events.FlagStatus = 'A' ORDER BY eventdates.EventDate, eventdates.TimeStart, events.Name"; $rsEvents = mysql_query($query_rsEvents, $connection1) or die(mysql_error()); $row_rsEvents = mysql_fetch_assoc($rsEvents); $totalRows_rsEvents = mysql_num_rows($rsEvents); obviously I have more fields here than I need for this question. Anyway, I want to total the number of events for each month, but I am not sure how to write the loop to work with the loop to give me the months? I do want to display all 12 months in my list - but add the totalEvents counter to those months that have events. Does this make sense. Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293715 Share on other sites More sharing options...
samoht Posted July 9, 2007 Author Share Posted July 9, 2007 i was thinking: for($t=0; $t<31; $t++){ $totalEvents = ($t+$row_rsEvents['EventDate']); echo "(" .$totalEvents. ")"; } but this just returns the next 30 years Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293731 Share on other sites More sharing options...
samoht Posted July 9, 2007 Author Share Posted July 9, 2007 Why doesn't this work?? <?php #This loop gets the next 6 months from current date $curr_month= $CurMonth; for($x=0;$x<6;$x++){ $month_num =($x+$curr_month > 12) ? $x+$curr_month -12 : $x+$curr_month; #get the total number of events for each month $i=0; do{$i++;} while ($row_rsEvents['EventDate'] = $curr_month); echo date('m',strtotime($month_num.'/01')); echo ' - '.date('F',strtotime($month_num.'/01'))." ( ". $i ." )<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59103-event-calendar-by-month-instead-of-day/#findComment-293772 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.