fife Posted July 16, 2013 Share Posted July 16, 2013 I am making a php calendar but struggling to get the query to work. The php calendar is made through a function with 3 variables fed into it. you can find the full calendar http://davidwalsh.name/php-event-calendar Anyway back to my variables. 3 of them.... $month, $year, $events Which are gotten as so.... /* date settings */$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y')); /* get all events for the given month */$events = array();mysql_select_db($database_dbconnect, $dbconnect);$query = "SELECT title,idcalendar, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM calendar WHERE event_date LIKE '$year-$month%'";$result = mysql_query($query, $dbconnect) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) {$events[$row['event_date']][] = $row;} Now I have my 3 vars I feed them into my function with the following line <?php echo draw_calendar($month,$year,$events); ?> Now within my function there is the following line to print out the event on any given day /* list the events */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);} As you can see if it finds an event it will show it and if it cant it will show 2 empty paragraph tags. I have an event in my database and under the field titled event_date and the event is for 2013-07-16 00:00:00 which is obviously today. If I use MySQL to run the query it pulls back my event quite happy. As soon as I run it in the webpage I get my empty paragraph tags back. Please help Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2013 Share Posted July 16, 2013 Where is the rest of your function? Quote Link to comment Share on other sites More sharing options...
fife Posted July 16, 2013 Author Share Posted July 16, 2013 The full function /* draws a calendar */function draw_calendar($month,$year,$events = array()){ /* draw table */$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">'; /* 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>'; $event_day = $year.'-'.$month.'-'.$list_day; /* list the events */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 = (int) ($_GET['month'] ? $_GET['month'] : date('m'));$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y')); /* 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>'; /* "next month" control */$next_month_link = '<a href="index.php?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="index.php?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>'; Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 16, 2013 Share Posted July 16, 2013 the format of the value in $event_day doesn't match what you are retreiving in the query. they must have exactly the same format (with leading zeros on the month and day and the same day format) for any kind of comparison to work. your query is also getting the day with the st, nd, rd..., which you probably don't want or need and which also won't match just the incrementing day number. also, since you are doing increment/decerments on the month to make the links, you are lossing the leading zero in the links. your best bet to format $event_day regardless of the input values would be to use - $event_day = sprintf('%04d-%02d-%02d',$year,$month,$list_day); Quote Link to comment Share on other sites More sharing options...
fife Posted July 16, 2013 Author Share Posted July 16, 2013 (edited) Ok I have changed my main query //from //SELECT title,idcalendar, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM calendar WHERE event_date LIKE '$year-$month%' //to SELECT title,idcalendar, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM calendar WHERE event_date LIKE '$year-$month%' which will output in the format 2013-07-16 I have also updated //from $event_day = $year.'-'.$month.'-'.$list_day; //to $event_day = sprintf('%04d-%02d-%02d',$year,$month,$list_day); The query again is working when type directly into phpmyadmin but the foreach loop on the page $event_day = sprintf('%04d-%02d-%02d',$year,$month,$list_day); /* list the events */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);} Still seems to be outputting 2 empty paragraph tags for the events. Is there anyway I can see if $event_day has values? Even in my query only before hand would do. //how do I make this line print the events here? while($row = mysql_fetch_assoc($result)) {$events[$row['event_date']][] = $row;} Edited July 16, 2013 by fife Quote Link to comment Share on other sites More sharing options...
kicken Posted July 16, 2013 Share Posted July 16, 2013 var_dump($events) after you create it. Then var_dump($event_day) in your loop. Check each output of event_day and make sure you can find it as a key in the output of the events array. 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.