I read dates from a table and show a thumbnail and link for each date. The thumbnails are in folder /impressie/yyyymmdd/ . The following script works fine if the folders do exist. I want, however, to catch the case that the folder does not exist (that is to say: wrong date in table)
As can be seen here, no exception is caught and the following message is shown
N.B. I am aware that all this code might not be great php-practice (too many years writing VBA!)
Code:
$result = $q->fetchAll(PDO::FETCH_ASSOC);
$numberOfEvents=count($result);
$numberOfRows=ceil($numberOfEvents/4);
$event=0;
for ($row = 0; $row < $numberOfRows; $rij++)
{
echo "<div class='w3-row'>\n";
for ($col = 0; $col < 4; $col++)
{
if ($event<$numberOfEvents)
{
$id=$result[$event]["id"];
$datum=$result[$event]["datum"];
$titel=$result[$event]["titel"];
try {
$thumbnail=glob(__DIR__ . "/impressie/". date_format(date_create($datum),"Ymd") . "/" . "*a.*")[0]; // <<================line 61
$thumbnail=str_replace(__DIR__,"",$thumbnail); <<================line 62
echo "<div class='w3-row w3-quarter plaatje'>\n";
echo "<a href='impressie.php?id=" . $id . "'>\n";
echo "<img src='" . $thumbnail . "' style='max-width:100%;' alt='".htmlspecialchars($titel)."'></a>";
$maandNr=date_format(date_create($datum),"m");
$maand=" ". substr("janfebmaraprmeijunjulaugsepoktnovdec", (($maandNr) * 3)-3,3)." ";
echo "<br>" . date_format(date_create($datum),"d").$maand.date_format(date_create($datum),"Y") . " - " . $titel . "<br><br>\n";
echo "</div>\n";
}
catch (PDOException $e)
{
echo "Exception caught";
}
}
$event++;
}
echo "</div>\n";
}
As can be seen here, no exception is caught and the following message is shown
Code:
Warning: Undefined array key 0 in xxxxxxx/phptest/impressies2.php on line 61
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /srv/customers/12250601/amsterdamhv.nl/phptest/impressies2.php on line 62
N.B. I am aware that all this code might not be great php-practice (too many years writing VBA!)