我正在开发这个日历,显示每个日期内的事件。用户看到的事件只是用户相关的事件。这种关系是通过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;
希望有人有解决方案
答案 0 :(得分:3)
你有一个拼写错误,初始化为cleanDateArray并且你正在传递cleanDataArray ... cleanDataArray没有在记录检索循环之外初始化。我只是改变你的初始化,你应该没事。
答案 1 :(得分:2)
在!empty()
检查之前,只需进行in_array
检查
if (!empty($cleanDataArray) && in_array($timestamp, $cleanDataArray)) {