0

我正在开发这个日历,显示每个日期内的事件。用户看到的事件只是与用户相关的事件。这种关系是通过 MySQL 多对多表建立的。它工作正常。只有当我的用户没有与他相关的事件时,我才会遇到问题。在这种情况下,in_array php 函数给了我这个错误:

注意:未定义变量:第 51 行 C:\xampp\htdocs\sistema-navalha\calendario_teste.php 中的 cleanDataArray

警告:in_array() 期望参数 2 为数组,在第 51 行的 C:\xampp\htdocs\sistema-navalha\calendario_teste.php 中给出 null

我知道问题是我的用户没有给我任何稍后将由in_array函数计算的值,但我无法找到一个合理的替代方案。在这种情况下重要的代码是这样的:

$stmt = $db->prepare("SELECT * FROM events WHERE
                      id_events = ( SELECT id_events FROM relationship_events WHERE id_user = ?)");
$stmt->bindParam(1, $idUser);
$stmt->execute();

$rawTimeStamps = $stmt->fetchAll(PDO::FETCH_ASSOC);
$cleanDateArray = array();

foreach ($rawTimeStamps as $t) {
$rawDate = $t['start'];
$rawDate = getdate($rawDate);
$cleanDate = mktime(0,0,0,$rawDate['mon'],$rawDate['mday'],$rawDate['year']);
$cleanDataArray[] = $cleanDate;
}

for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    
    $calendar.= '<td class="calendar-day">';
        $timestamp = mktime(0,0,0,$month,$list_day,$year);
        if (in_array($timestamp, $cleanDataArray)) {

        $date = getdate($timestamp);
        $time_start = mktime(0,0,0,$date['mon'],$date['mday'],$date['year']);
        $time_end = mktime(23,59,59,$date['mon'],$date['mday'],$date['year']);
        $stmt = $db->prepare('SELECT title FROM events WHERE start BETWEEN ? AND ?');
        $stmt->bindParam(1,$time_start,PDO::PARAM_INT);
        $stmt->bindParam(2,$time_end,PDO::PARAM_INT);
        $stmt->execute();
        $events = $stmt->fetch(PDO::FETCH_ASSOC);

        $calendar.= '<div class="day-number day-number-event"><a href="#">'.$list_day.'</a></div><p>'.$events["title"].'</p>';
        } else {
        $calendar.= '<div class="day-number day-number-noevent">'.$list_day.'</div><div id="calendar-events"></div>';
        }

    $calendar.= '</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;

希望有人有解决方案

4

2 回答 2

3

您有一个拼写错误,初始化为 cleanDateArray 并且您正在传递 cleanDataArray...cleanDataArray 未在记录检索循环之外初始化。我只是改变你的初始化,你应该没问题。

于 2012-11-05T19:38:46.947 回答
2

只需在!empty()支票前放一张支票in_array

if (!empty($cleanDataArray) && in_array($timestamp, $cleanDataArray)) {
于 2012-11-05T19:38:45.443 回答