I’m developing this calendar that show events inside each date. The events that the user sees is just the events that the user is related. The relationship is made via MySQL many-to-many table. It is working fine. I am having problem only when my user has no events related to him. In that case, the in_array php function gives me this error:
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
I understand that the problem is that my user gives me no value that later will be calculated by in_array function, but I’m not able to figure out a reasonable alternative. The code that matters in this situation is this:
$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;
Hope someone have a solution
You have a misspelling, initialized as cleanDateArray and you’re passing cleanDataArray…cleanDataArray is not initialized outside of the record retrieval loop. I would just change your initialization and you should be fine.