mongoose0031800 Posted July 25, 2011 Share Posted July 25, 2011 This is my code so far.. <section id="content" class="planner"> <h2> <?php $date_db = strtotime("07/01/2011"); echo date("F Y", $date_db) ?> </h2> <table class="month"> <tr class="days"> <td>Mon</td> <td>Tues</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> <td>Sun</td> </tr> <?php $today = date("d", $date_db); // Current day $month = date("m", $date_db); // Current month $year = date("Y", $date_db); // Current year //die($today); /* $today = date("d"); // Current day $month = date("m"); // Current month $year = date("Y"); // Current year */ $days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month $lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month $start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month $finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month $laststart = $start - 1; // Days of previous month in calander $counter = 1; $nextMonthCounter = 1; //if the start day is greater than a Friday; if it is, we have 6 rows, otherwise we have 5 if($start > 5){ $rows = 6; }else {$rows = 5; } //we then have a nested loop - one for the weeks, and one for the days in the weeks for($i = 1; $i <= $rows; $i++){ echo '<tr class="week">'; for($x = 1; $x <= 7; $x++){ if(($counter - $start) < 0){ $date = (($lastmonth - $laststart) + $counter); $class = 'class="blur"'; echo '<td '.$class.'><a class="date"></a></td>'; }else if(($counter - $start) >= $days){ $date = ($nextMonthCounter); $nextMonthCounter++; $class = 'class="blur"'; echo '<td '.$class.'><a class="date"></a></td>'; }else { $date = ($counter - $start + 1); if($today == $counter - $start + 1){ $class = 'class="today"'; } echo '<td '.$class.'><a class="date">'. $date . '</a></td>'; } $counter++; $class = ''; } echo '</tr>'; } ?> </table> </section> I've got it outputting one month so far but not organizing the items into it and not showing 5 months instead of one. Most of that code is from a tutorial. What I want to do is output 5 months (current and 4 in the future) and loop through items in my database and have PHP sort the items into the calendar. Any advice on where I go from here? PHPFREAKS ROCKS! Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/ Share on other sites More sharing options...
mongoose0031800 Posted July 25, 2011 Author Share Posted July 25, 2011 Is there anything else I need to provide to help? Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247032 Share on other sites More sharing options...
jcbones Posted July 25, 2011 Share Posted July 25, 2011 I'll look over it, give me a bit. Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247055 Share on other sites More sharing options...
mongoose0031800 Posted July 25, 2011 Author Share Posted July 25, 2011 OK, also I have another version but it is completely different from the one above if you would like to see it. Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247056 Share on other sites More sharing options...
jcbones Posted July 25, 2011 Share Posted July 25, 2011 Here is your script, showing the current month, and 5 in the future. In order to incorporate your database, you need to provide a database dump of your table structure, as well as some test data. <section id="content" class="planner"> <h2> <?php $date_db = strtotime("07/01/2011"); echo date("F Y", $date_db); ?> </h2> <?php $today = date("d", $date_db); // Current day $month = date("m", $date_db); // Current month $year = date("Y", $date_db); // Current year for($n = 0; $n < 5; $n++,$month++) { ?> <table class="month"> <tr class="days"> <td>Mon</td> <td>Tues</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> <td>Sun</td> </tr> <?php //die($today); /* $today = date("d"); // Current day $month = date("m"); // Current month $year = date("Y"); // Current year */ $days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month $lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month $start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month $finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month $laststart = $start - 1; // Days of previous month in calander $counter = 1; $nextMonthCounter = 1; //if the start day is greater than a Friday; if it is, we have 6 rows, otherwise we have 5 if($start > 5){ $rows = 6; }else {$rows = 5; } //we then have a nested loop - one for the weeks, and one for the days in the weeks for($i = 1; $i <= $rows; $i++){ echo '<tr class="week">'."\n"; for($x = 1; $x <= 7; $x++){ if(($counter - $start) < 0){ $date = (($lastmonth - $laststart) + $counter); $class = 'class="blur"'; echo '<td '.$class.'><a class="date"></a></td>'."\n"; }else if(($counter - $start) >= $days){ $date = ($nextMonthCounter); $nextMonthCounter++; $class = 'class="blur"'; echo '<td '.$class.'><a class="date"></a></td>'."\n"; }else { $date = ($counter - $start + 1); if($today == $counter - $start + 1){ $class = 'class="today"'; } echo '<td '.$class.'><a class="date">'. $date . '</a></td>'."\n"; } $counter++; $class = ''; } echo '</tr>'."\n"; } ?> </table> <?php } ?> </section> Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247062 Share on other sites More sharing options...
PFMaBiSmAd Posted July 25, 2011 Share Posted July 25, 2011 output 5 months (current and 4 in the future) ^^^ To do that, you should convert the current code into a function that accepts the year and month you want to display. You would then simply loop the correct number of times and call the function - <?php function display_month($year,$month,$data){ // you code that displays a calendar for the year and month that was supplied... } list($year,$month) = explode('-',date('Y-m')); // get the starting year, month for($x = 0; $x <= 4; $x++){ display_month($year,$month,$data); list($year,$month) = explode('-',date('Y-m',strtotime("$year-$month-01 + 1 month"))); // get the next year, month } ?> The $data array in the above code is missing, read on to find out what it should be - organizing the items into it ^^^ To do that, you should execute one query that gets all the data you want from your database table and store it into the $data array that gets passed to the display_month() function. I would use the date "yyyy-mm-dd" as the array index. See the two threads that the following posts of mine are in for more information - http://www.phpfreaks.com/forums/index.php?topic=338516.msg1595240#msg1595240 http://www.phpfreaks.com/forums/index.php?topic=330865.msg1557351#msg1557351 Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247064 Share on other sites More sharing options...
mongoose0031800 Posted July 27, 2011 Author Share Posted July 27, 2011 Alright guys, here is my final working version..tell me what you think. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>PHP Calendar</title> <!--[if IE]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <style type="text/css"> div.day-number { background:#999; position:absolute; z-index:2; top:-5px; right:-25px; padding:5px; color:#fff; font-weight:bold; font-size:12px; width:20px; text-align:center; } td.calendar-day, td.calendar-day-np { width:120px; padding:5px 25px 5px 5px; border-bottom:1px solid #999; border-right:1px solid #999; font-size:12px; } table.calendar td { } </style> </head> <body> <?php /* draws a calendar */ function draw_calendar($month,$year,$events = array()){ /* draw table */ $calendar = '<table cellpadding="0" cellspacing="0" class="calendar" width="634px">'; /* table headings */ $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>'; /* days and weeks vars now ... */ $running_day = date('w',mktime(0,0,0,$month,1,$year)); $days_in_month = date('t',mktime(0,0,0,$month,1,$year)); $days_in_this_week = 1; $day_counter = 0; $dates_array = array(); /* row for week one */ $calendar.= '<tr class="calendar-row">'; /* print "blank" days until the first of the current week */ for($x = 0; $x < $running_day; $x++): $calendar.= '<td class="calendar-day-np"> </td>'; $days_in_this_week++; endfor; /* keep going with days.... */ for($list_day = 1; $list_day <= $days_in_month; $list_day++): $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">'; /* add in the day number */ $calendar.= '<div class="day-number">'.$list_day.'</div>'; if($list_day <= 9) { $event_day = $year.'-'.$month.'-0'.$list_day; } else { $event_day = $year.'-'.$month.'-'.$list_day; } if(isset($events[$event_day])) { foreach($events[$event_day] as $event) { $calendar.= '<div class="event">'.$event['title'].'</div>'; } } else { $calendar.= str_repeat('<p> </p>',2); } $calendar.= '</div></td>'; if($running_day == 6): $calendar.= '</tr>'; if(($day_counter+1) != $days_in_month): $calendar.= '<tr class="calendar-row">'; endif; $running_day = -1; $days_in_this_week = 0; endif; $days_in_this_week++; $running_day++; $day_counter++; endfor; /* finish the rest of the days in the week */ if($days_in_this_week < : for($x = 1; $x <= (8 - $days_in_this_week); $x++): $calendar.= '<td class="calendar-day-np"> </td>'; endfor; endif; /* final row */ $calendar.= '</tr>'; /* end the table */ $calendar.= '</table>'; /** DEBUG **/ $calendar = str_replace('</td>','</td>'."\n",$calendar); $calendar = str_replace('</tr>','</tr>'."\n",$calendar); /* all done, return result */ return $calendar; } function random_number() { srand(time()); return (rand() % 7); } /* date settings */ $month = ($_GET['month'] ? $_GET['month'] : date('m')); $year = ($_GET['year'] ? $_GET['year'] : date('Y')); /* select month control */ $select_month_control = '<select name="month" id="month">'; for($x = 1; $x <= 12; $x++) { if($x <= 9) $x = '0'.$x; $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>'; } $select_month_control.= '</select>'; /* select year control */ $year_range = 7; $select_year_control = '<select name="year" id="year">'; for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) { $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>'; } $select_year_control.= '</select>'; /* "next month" control */ $next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month >></a>'; /* "previous month" control */ $previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control"><< Previous Month</a>'; /* bringing the controls together */ //$controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> '.$previous_month_link.' '.$next_month_link.' </form>'; $controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> </form>'; require_once('scripts/dbconfig.php'); /* get all events for the given month */ $events = array(); $query = "SELECT *, DATE_FORMAT(date,'%Y-%m-%d') AS event_date FROM shows WHERE DATE_FORMAT(date,'%Y-%m') LIKE '".$year."-".$month."'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $events[$row['event_date']][] = $row; } echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>'; echo '<div style="float:left;">'.$controls.'</div>'; echo '<div style="clear:both;"></div>'; echo draw_calendar($month,$year,$events); echo '<br /><br />'; /* echo $query; echo '<pre>'; print_r($events); echo '</pre>'; */ ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/242799-php-outputting-a-calendar-and-sorting-items-into-it/#findComment-1247737 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.