gpt4 book ai didi

php - 当变量为空时使用 IN_ARRAY 获取错误

转载 作者:行者123 更新时间:2023-12-01 00:53:30 25 4
gpt4 key购买 nike

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

Notice: Undefined variable: cleanDataArray in C:\xampp\htdocs\sistema-navalha\calendario_teste.php on line 51

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\sistema-navalha\calendario_teste.php on line 51

我知道问题是我的用户没有给我任何稍后将由 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;

希望有人能解决

最佳答案

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

关于php - 当变量为空时使用 IN_ARRAY 获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13239044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com