为什么成功查询后日历上没有显示任何事件?

时间:2016-08-12 16:23:53

标签: php pdo calendar

像我在这里读到的许多其他人一样,我正在尝试使用Davild Walsh's calendar tutorial的更新形式制作基于php / sql的事件日历。

我似乎正好连接到数据库,甚至填充$ bookings和$ events数组。但是我的日历网格中没有出现任何事件。有人可以帮我看看我错过了什么,为什么日历没有填充事件?我现在甚至没有做一个复杂的选择陈述...我只是选择了所有的事件......成功...所以我认为它必须是一些小东西,一些细微的细节丢失或丢掉的东西。任何帮助都感激不尽。参考代码如下:

使用PDO成功连接到数据库,但发布以供参考:

 try {
 $bdd = new PDO('mysql:host=*****;dbname=*****', '*****', '***');
 } catch(Exception $e) {
  exit('Unable to connect to database.');
 }

像这样查询数据库:

$events = array();
 $query = "SELECT * FROM oipevents";
 try {
 $result = $bdd->query($query) or die(print_r($bdd->errorInfo()));
 } catch (Exception $e) {
     echo "no results";
     exit;
 }
 $bookings = $result->fetchAll(PDO::FETCH_ASSOC);

 foreach ($bookings as $row) {
     $events[$row['event_date']][] = $row;
 } 

然后使用以下代码绘制日历(注意,带控件的日历显示正常,没有任何事件显示):

/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

    /* draw table */
    $calendar = '<div class="cal_table">';

    /* table headings */
    $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    $calendar.= '<div class="cal_table_cell theme_blue_secondary">'.implode('</div><div class="cal_table_cell theme_blue_secondary">',$headings).'</div></div><!-- #cal_table -->';

    /* 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.= '<div class="cal_table">';

    /* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<div class="cal_table_cell">&nbsp;</div>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<div class="cal_table_cell"><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;

            if(isset($events[$event_day])) {
                foreach($events[$event_day] as $event) {
                //  $calendar.= '<div class="event">'.$event['title'].'</div>';
                    $calendar.='<div class="event"><a href="?event='.$event['title'].'">'.$event['start'].' - '.$event['tijdtot'].'</span></a></div>';
                }
            }
            else {
                $calendar.= str_repeat('',2);
            }
        $calendar.= '</div></div><!-- #cal_table_cel -->';
        if($running_day == 6):
            $calendar.= '</div><!-- #cal_table -->';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<div class="cal_table">';
            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.= '<div class="cal_table_cell">&nbsp;</div><!-- #cal_table_cell -->';
        endfor;
    endif;

    /* final row */
    $calendar.= '</div><!-- #cal_table -->';



    /** DEBUG **/
    $calendar = str_replace('</div>','</div>'."\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'));

    if($month < 10){
        $month = '0'.$month;
    }
/* 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 &gt;&gt;</a>';

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">&lt;&lt;    Previous Month</a>';


/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';


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 />'; 

有人知道为什么活动没有进入日历吗? 当我执行print_r($ bookings)和print_r($ events)时,我可以看到填充的数组 - 那么它是什么?我是否应该发布数组以供参考?好 ... 来自print_r的$ bookings数组($ bookings):

  

数组([0] =&gt;数组([id] =&gt; 1 [标题] =&gt;测试事件[event_date]   =&GT; 2016-07-27 09:30:00 [end] =&gt; 2016-07-27 10:00:00 [url] =&gt; http://www.auburn.edu [allDay] =&gt; false [description] =&gt;测试事件   [department] =&gt;校友)...)

来自print_r的事件数组($ events):

  

Array([2016-07-27 09:30:00] =&gt;数组([0] =&gt;数组([id] =&gt; 1   [title] =&gt;测试事件[event_date] =&gt; 2016-07-27 09:30:00 [end] =&gt;   2016-07-27 10:00:00 [url] =&gt; http://www.auburn.edu [allDay] =&gt;假   [描述] =&gt;测试事件[部门] =&gt;校友))..)

请帮助,谢谢!

0 个答案:

没有答案