我正在尝试使用我的网站(Wordpress)上的事件构建自定义日历。我能够获取事件和所有信息,下面的代码在localhost上可正常使用当年,但是,它不适用于实时站点。但是,即使在本地主机上,也无法选择月份,年份。请帮助我解决此问题:
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:100px;">';
/* add in the day number */
/* 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">'.$list_day.'</div>';
$event_day = $year.'-'.$month.'-'.$list_day;
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event" style="background:yellow">'.'Couple-'.$event['title'].'</br>Wedding_id-'.$event['id'].'</br>Email-'.$event['email'].'</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 < 8):
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;
}
/* date settings */
if(empty($_GET['month']) || empty($_GET['year'])) {
$month = date('m');
$year = date('Y');
}
else {
$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="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control" style="color:black;background:white;">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" style="color:black;background:white;"><< 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 */
global $wpdb;
$table_name = $wpdb->prefix . 'contract';
$table_name1 = $wpdb->prefix . 'wedding';
$month = str_pad($month,2,"0", STR_PAD_LEFT);
$events = array();
$results = $wpdb->get_results( "SELECT c.cname AS title,c.id AS id,c.email as email, DATE_FORMAT(w.wdate,'%Y-%m-%d') AS event_date FROM $table_name c,$table_name1 w WHERE w.wdate LIKE '$year-$month%' and w.id=c.wedding_id",ARRAY_A);
foreach ( $results as $row ) {
$events[$row['event_date']][] = $row;
}
HTML:
<div id="primary" class=" col-xs-12 col-md-8" >
<div id="calendar_div" style="padding:20px">
<?php
echo '<h2 style="float:left; padding-right:30px;color:black;background:white;">'.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 />';?>
</div>
</div>
答案 0 :(得分:0)
自从我自己找到解决方案以来,我想发布一个答案,这可能会对在这种情况下的其他人有所帮助。但是,我在WordPress中使用了自定义会话,这可能不是最好的或推荐的方式,但是该网站是内部网站,不需要少数员工使用,因此我决定采用这种方式。
问题是Wordpress不允许在URL中使用Year,这干扰了我的整体工作:
这是代码: PHP:
function draw_calendar($calmonth,$calyear,$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,$calmonth,1,$calyear));
$days_in_month = date('t',mktime(0,0,0,$calmonth,1,$calyear));
$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 */
/* 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">'.$list_day.'</div>';
$event_day = $calyear.'-'.$calmonth.'-'.$list_day;
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event" style="background:yellow">'.'Couple-'.$event['title'].'</br>Wedding_id-'.$event['id'].'</br>Email-'.$event['email'].'</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 < 8):
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;
}
/*Getting the Date */
if (empty($_GET['calmonth']) ||empty($_GET['calyear']) ){
$calmonth = date('m');
$calyear = date('Y');}
else {
$_SESSION['calmonth'] = $_GET['calmonth'];
$_SESSION['calyear'] = $_GET['calyear'];
$calmonth = $_SESSION['calmonth'];
$calyear = $_SESSION['calyear'];}
/* select month control */
$select_month_control = '<select name="calmonth" id="calmonth">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $calmonth ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$calyear)).'</option>';
}
$select_month_control.= '</select>';
/* select year control */
$year_range = 7;
$select_year_control = '<select name="calyear" id="calyear">';
for($x = ($calyear-floor($year_range/2)); $x <= ($calyear+floor($year_range/2)); $x++) {
$select_year_control.= '<option value="'.$x.'"'.($x != $calyear ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';
/* "next month" control */
$next_month_link = '<a href="?calmonth='.($calmonth != 12 ? $calmonth + 1 : 1).'&calyear='.($calmonth != 12 ? $calyear : $calyear + 1).'" class="control" style="color:black;background:white;">Next Month >></a>';
/* "previous month" control */
$previous_month_link = '<a href="?calmonth='.($calmonth != 1 ? $calmonth - 1 : 12).'&calyear='.($calmonth != 1 ? $calyear : $calyear - 1).'" class="control" style="color:black;background:white;"><< 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 */
global $wpdb;
$table_name = $wpdb->prefix . 'contract';
$table_name1 = $wpdb->prefix . 'wedding';
$calmonth = str_pad($calmonth,2,"0", STR_PAD_LEFT);
$events = array();
$results = $wpdb->get_results( "SELECT c.cname AS title,c.id AS id,c.email as email, DATE_FORMAT(w.wdate,'%Y-%m-%d') AS event_date FROM $table_name c,$table_name1 w WHERE w.wdate LIKE '$calyear-$calmonth%' and w.id=c.wedding_id",ARRAY_A);
foreach ( $results as $row ) {
$events[$row['event_date']][] = $row;
}
HTML:
<div id="primary" class=" col-xs-12 col-md-8" >
<div id="calendar_div" style="padding:20px">
<?php
echo '<h2 style="float:left; padding-right:30px;color:black;background:white;">'.date('F',mktime(0,0,0,$calmonth,1,$calyear)).' '.$calyear.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($calmonth,$calyear,$events);
echo '<br /><br />';?>
</div>
</div>