brindha Posted August 7, 2012 Share Posted August 7, 2012 Hi, I used this above draw_calendar function i can't get events from db. But calendar is display fine. [mod:edit the OP is referring to the code found in this thread - http://forums.phpfreaks.com/index.php?topic=363483.0] my code is <?php 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:60px;">'; /* add in the day number */ $calendar.= '<div class="day-number">'.$list_day.'</div>'; $event_day = $year.'-'.$month.'-'.$list_day; if(isset($event_day)) { foreach($event_day as $event) { $calendar.= '<div class="event">'.$event['title'].'</div>'; } } else { echo "hi2"; $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) (isset($_GET['month']) ? $_GET['month'] : date('m')); $year = (int) (isset($_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="?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>'; /* get all events for the given month */ $events = array(); $query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM events WHERE event_date LIKE '$year-$month%'"; $result = mysql_query($query,$con) or die('cannot get results!'); 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 />'; Please help me. Thanks. mod edit: added code tags Quote Link to comment https://forums.phpfreaks.com/topic/266763-events-calendar-code-not-working/ Share on other sites More sharing options...
Christian F. Posted August 7, 2012 Share Posted August 7, 2012 [code][/code] blocks; Learn them, use them, love them. Thanks. Makes the code and the post a whole lot easier to read, and increases the chances of someone wanting to help you. Quote Link to comment https://forums.phpfreaks.com/topic/266763-events-calendar-code-not-working/#findComment-1367425 Share on other sites More sharing options...
brindha Posted August 7, 2012 Author Share Posted August 7, 2012 Hi, I used this above draw_calendar function i can't get events from db. But calendar is display fine. my code is 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:60px;">'; /* add in the day number */ $calendar.= '<div class="day-number">'.$list_day.'</div>'; $event_day = $year.'-'.$month.'-'.$list_day; if(isset($event_day)) { foreach($event_day as $event) { $calendar.= '<div class="event">'.$event['title'].'</div>'; } } else { echo "hi2"; $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) (isset($_GET['month']) ? $_GET['month'] : date('m')); $year = (int) (isset($_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="?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>'; /* get all events for the given month */ $events = array(); $query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM events WHERE event_date LIKE '$year-$month%'"; $result = mysql_query($query,$con) or die('cannot get results!'); 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 />'; Please help me. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/266763-events-calendar-code-not-working/#findComment-1367439 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2012 Share Posted August 7, 2012 You need to start your own thread for your own programming problem, no matter how close your code might be to someone else's, so that you can find and track your own threads. I have split your posts on this subject into their own thread... Quote Link to comment https://forums.phpfreaks.com/topic/266763-events-calendar-code-not-working/#findComment-1367441 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2012 Share Posted August 7, 2012 i can't get events from db ^^^ You do realize that statement contains no useful information upon which to help you. You would need to write or post a picture of the exact symptom or error you got that leads you to believe you cannot get events from the db in order for anyone who is not standing right next to you to have a chance at helping you find the cause of the problem. Also, what debugging steps have you taken to try and pin down exactly why you cannot get events from the db? Are you getting mysql errors? Are you getting any php detected errors? Do you have data in your database table that matches the year and month that you are querying for? Quote Link to comment https://forums.phpfreaks.com/topic/266763-events-calendar-code-not-working/#findComment-1367444 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.