每个循环使用两个with else statment

时间:2016-04-28 16:23:37

标签: php

我有两个数组,其中包含图像的位置数组。当我为每个循环使用第一个时,我从位置获取图像。如果我使用其他声明,则sript无效 我使用以下数组作为$ 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++;
        }
    }

1 个答案:

答案 0 :(得分:0)

  

如果您之前使用 IF 语句,则可以使用 ELSE 语句。您的案例中将显示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);
}