Kritter5x Posted March 28, 2013 Share Posted March 28, 2013 Hi, I have built an event calendar and whipped up some basic event filters to use in it. All of that works fine, but i've had people complain that as soon as they switch pages, it resets any filters or months they might have jumped too. It was suggested to use Setcookie functions to keep chosen filters etc, but I can't for the life of me get it working properly no matter where or how I use them. I'm still learning most of this stuff and i'd like to get this working. Any suggestions or taking a look at my code would be fantastic. function draw_calendar($month,$year,$events = array()){ /* draw table */ $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">'; /* table headings */ $headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); $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; for($list_day = 1; $list_day <= $days_in_month; $list_day++): $calendar.= '<td '; $calendar.= 'class="calendar-day"';; $calendar.= '><div style="height:30px; float: left;">'; /* add in the day number */ if($list_day < 10) { $list_day = str_pad($list_day, 2, '0', STR_PAD_LEFT); } $calendar.= '<div class="day-number"><a '; if ($list_day == date('d') && $month == date('m') && $year == date('Y')){ $calendar.= 'class="today"'; } else { $calendar.= ''; } //$calendar.='href="#" id="pop">'.$list_day.'</a></div>'; $calendar.='href=upcomingevents.php?m='.$month.'&d='.$list_day.'&y='.$year.'>'.$list_day.'</a></div>'; // <a href="#" id="pop" >PopUp</a> // <br /> $event_day = $year.'-'.$month.'-'.$list_day; //Gettingn the event category $event_category = $_GET['category']; if(isset($events[$event_day])) { foreach($events[$event_day]as $event) { $calendar.= '<div class="event_'.$event['event_category'].'"></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 = (int) ($_GET['month'] ? $_GET['month'] : date('m')); $year = (int) ($_GET['year'] ? $_GET['year'] : date('Y')); $category = ($_GET['event_category']); /* select month control */ $select_month_control = '<select name="month" id="month">'; for($x = 1; $x <= 12; $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>'; /* select category control */ // Array contents $categories = array('All','Touring','Motorsport','Club'); $cat_selected= (isset($_GET['category'])) ? ((int) $_GET['category']) : 0; $select_category_control= '<select name="category" id="category">'; foreach ($categories as $key => $val) { if ($cat_selected==$key) $sel='selected '; else $sel= ''; $select_category_control.= "<option {$sel}value=\"{$key}\">{$val}</option>"; } $select_category_control.= '</select>'; if (($_GET["category"]== 0)){ $selected = "All"; } elseif (($_GET["category"]== 1)){ $selected = "Touring"; } elseif (($_GET["category"]== 2)){ $selected = "Motorsport"; } elseif (($_GET["category"]== 3)){ $selected = "Club"; } /* "next month" control */ $next_month_link = '<a class="butLink" href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'&category='.($_GET["category"]).'" class="control">»</a>'; /* "previous month" control */ $previous_month_link = '<a class="butLink" href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'&category='.($_GET["category"]).'" class="control">«</a>'; /* bringing the controls together */ $controls3 = '<form method="get">'.$select_month_control.$select_year_control.$select_category_control.' <input class="button" type="submit" name="submit" value="Select Event Type" /></form>'; /* get all events for the given month */ /* get all events for the given month */ $month = str_pad($month,2,'0', STR_PAD_LEFT); //$month = str_pad($list_day, 2, '0', STR_PAD_LEFT); $events = array(); if (($_GET["category"]== 0)){ $query = "SELECT event_name, event_category, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '$year-$month%' AND approved='yes'"; } else{ $query = "SELECT event_name, event_category, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_category LIKE '$selected' AND event_date LIKE '$year-$month%' AND approved='yes'"; } $result = mysql_query($query,$mysql) or die('cannot get results!'); while($row = mysql_fetch_assoc($result)) { $events[$row['event_date']][] = $row; } echo '<div id="calHeadBar">'; echo '<div id="prevLink">'.$previous_month_link.'</div>'; echo '<div id="midLink"><p style="font-size: 11px; font-weight: bold; padding:0;">'.$selected.' events for<br /> '.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</p></div>'; echo '<div id="nextLink">'.$next_month_link.'</div>'; echo '</div>'; echo '<div style="clear:both;"></div>'; echo '<div id="calBox">'; echo draw_calendar($month,$year,$events); echo '</div>'; echo '<div id="calHeadBar2">'; echo '<div class="pushMe">'; echo '<div class="box">'; echo '<div style="width: 10px; height: 10px; background:#f83a22; position: relative; margin-right: 5px;"></div><p class="noPadding">Touring</p>'; echo '<div style="width: 10px; height: 10px; background:#7bd148; position: relative; margin-right: 5px;"></div><p class="noPadding">Motorsport</p>'; echo '<div style="width: 10px; height: 10px; background:#89b3d7; position: relative; margin-right: 5px;"></div><p class="noPadding">Club</p>'; echo '</div>'; echo '</div>'; echo '<div style="float:left;">'.$controls3.'</div>'; echo '</div>'; Quote Link to comment Share on other sites More sharing options...
ulferik Posted March 28, 2013 Share Posted March 28, 2013 Try storing it in a session. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 28, 2013 Share Posted March 28, 2013 pass your filters that determine what is displayed when you make a get request for a page as $_GET parameters in the url. that's what $_GET parameters are designed to do. this will allow someone to bookmark the page and return to the same point later or to share a link to the results or to even try different searches and can navigate back to any point as long as the visited pages are still in their browser history. Quote Link to comment Share on other sites More sharing options...
Kritter5x Posted March 29, 2013 Author Share Posted March 29, 2013 Unfortunately i'm not sure how to really do either of those suggestions, at least in the code I have. 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.