0

I have the below php script working if you use one condition (if..else works fine) If try to use if and else if statement( because of two conditions are true) the second one is working and not the first one.

Script:

$damage_topdir = "/ids_images/drsIN2/";
$damage_topdir1 = "/ids_images/drsIN1/";

$tmp = split(" ", $displayEntryDatetime);
$date = $tmp[0];
$time = $tmp[1];

$tmp = split("/", $date);
$day = $tmp[0];
$month = $tmp[1];
$year = $tmp[2];

$displayEntryDatetime = $year."-".$month."-".$day." ".$time;

$cam_list = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

if ($cam = 'DRHdrsIN1')
{
foreach ($cam_list as $cam) {
  $timestamp = strtotime($displayEntryDatetime);
  $cam_delta =6;
  $timestamp = $timestamp - $cam_delta;
  for ($i = 0; $i < $cam_delta+20; $i++) {
    $cdate = date("d_m_Y* H_i_s", $timestamp);
    $image_name = "/xampp/htdocs" . $damage_topdir. $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++;
  }
}
}

elseif($cam = 'DRHdrsIN2') ----> this one executes normally...
{
    foreach ($cam_list as $cam) {
  $timestamp = strtotime($displayEntryDatetime);
  $cam_delta = 6;
  $timestamp = $timestamp - $cam_delta;

  for ($i = 0; $i < $cam_delta+20; $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 {

}
?>
Kiran V
  • 69
  • 5
  • 16
  • 5
    `=` is aignment; `==` or `===` are comparison operators – Mark Baker May 04 '16 at 14:27
  • 1
    Possible duplicate of [The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) – fusion3k May 04 '16 at 14:30
  • I have used different comparision operators but only second elseif staments works... – Kiran V May 04 '16 at 14:37
  • The code, as it is, will never execute the `else` branch, because the **assignment** `$cam = 'DRHdrsIN2'` always gives boolean true, because of not giving an empty string (or any other representation of boolean false). – syck May 04 '16 at 14:39
  • Yes @syck .. the first condition will be always true if you are using `=`. So there is no chance for the elseif to be executed. – Sabin Chacko May 04 '16 at 14:42
  • @RossiMilanBob Yep, just seen it, the `if` itself suffers from the same ailment too. Dunno why I am always reading code from bottom to top. =) – syck May 04 '16 at 14:49

1 Answers1

0

You ned to use a comparison, not an assignment:

if ($cam == 'DRHdrsIN1'){
    ....
elseif($cam == 'DRHdrsIN2'){
    ...
Webomatik
  • 844
  • 7
  • 7