我有两个数组,其中Images的位置数组。当我对每个循环使用第一个时,我会从位置获得图像。如果我使用else语句,srpt将不起作用
我使用以下数组作为$image_name。
任何想法。
$cam_list1 = array(1,2,3,4,5,6,7,8);
$cam_list2 = array(9,10,11,12,13,14,15,16);
foreach ($cam_list1 as $cam) {
$timestamp = strtotime($displayEntryDatetime);
$cam_delta = 14;
$timestamp = $timestamp - $cam_delta;
for ($i = 0; $i < $cam_delta+2; $i++) {
$cdate = date("d_m_Y* H_i_s", $timestamp);
$image_name = "/xampp/htdocs" . $damage_topdir1. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
foreach (glob($image_name) as $filename) {
if (file_exists($filename)) {
$fs_image = str_replace("/xampp/htdocs", "", $filename);
print "<h3>Camera $cam</h3>";
print "<a href='"$fs_image'" target='"_new'"><img src='"$fs_image'" height=240 width=320 /></a>'n";
}
}
$timestamp++;
}
}
else
foreach ($cam_list2 as $cam)
{
$timestamp = strtotime($displayEntryDatetime);
$cam_delta = 14;
$timestamp = $timestamp - $cam_delta;
for ($i = 0; $i < $cam_delta+2; $i++) {
$cdate = date("d_m_Y* H_i_s", $timestamp);
$image_name = "/xampp/htdocs" . $damage_topdir2. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
foreach (glob($image_name) as $filename) {
if (file_exists($filename)) {
$fs_image = str_replace("/xampp/htdocs", "", $filename);
print "<h3>Camera $cam</h3>";
print "<a href='"$fs_image'" target='"_new'"><img src='"$fs_image'" height=240 width=320 /></a>'n";
}
}
$timestamp++;
}
}
如果您以前使用过if语句,则可以使用ELSE声明ONLY。在您的案例中会显示一个PHP致命错误。
我不清楚你想做什么,但如果你想检查你的列表中是否有图像,你可以在cam_list中使用一个函数来执行foreach过程。
function getImages($list) {
$inList = false;
foreach ($list as $cam) {
$timestamp = strtotime($displayEntryDatetime);
$cam_delta = 14;
$timestamp = $timestamp - $cam_delta;
for ($i = 0; $i < $cam_delta+2; $i++) {
$cdate = date("d_m_Y* H_i_s", $timestamp);
$image_name = "/xampp/htdocs" . $damage_topdir1. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
foreach (glob($image_name) as $filename) {
if (file_exists($filename)) {
$inList = true;
$fs_image = str_replace("/xampp/htdocs", "", $filename);
print "<h3>Camera $cam</h3>";
print "<a href='"$fs_image'" target='"_new'"><img src='"$fs_image'" height=240 width=320 /></a>'n";
}
}
$timestamp++;
}
}
return ($inList) ? true : false;
}
使用if/else,您可以在cam_list1或cam_list2中获取图像。
$cam_list1 = array(1,2,3,4,5,6,7,8);
$cam_list2 = array(9,10,11,12,13,14,15,16);
if (!getImages($cam_list1)) {
getImages($cam_list2);
}