-1

I am trying to write a script that displays images and/or mp4 files. The image and mp4 file names are stroed in a data table. When the script runs it needs to loop through the array returned by the mySQL statement. Each returned record has a column called "FileType" which contains either a 1 for an image file or 2 for an mp4 file.

My thinking is that as the do while loop is executed it looks at the content of "FileType" it is trapped by the if statement and uses the appropriate code to display the file. The issue I have is the script is only displaing rows where the column "FileType" contains 1.

So if the first row contains an image file, it displays, but if the row contains an mp4 file it is not displayed.

Can anyone see where I have gone wrong and how I can fix it so it loops through the array of rows and displays each one.

My code:

   do { 
 
   $FileType = $row_signage['FileType'];
   if($row['DisplayType'] == "P" ) {
       $DisplayWdith = "1080";
       $DisplayHeight = "1920";
   } else { 
       $DisplayWdith = "1920";
       $DisplayHeight = "1080";   
   }
  ?>
  <div>
    <?php if($FileType == 1) { ?>
      <img src="/<?=$ImagePath . $row_signage['SignageImageName']?>" class="responsive"/>
    <?php } elseif($FileType == 2) { ?>
      <div id="video_player">
        <video width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
            <source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
        </video>
    <?php } ?>
  </div>
    } while ($row_signage = mysqli_fetch_array($fb)); ?>
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • I think this is basically [do vs while](https://stackoverflow.com/q/224059/231316), unless I'm missing something, and you should be using `while` – Chris Haas Nov 12 '21 at 18:05
  • @Chris Haas Hi and thanks for your reply. I have tried using while but still the same result, only displays one of the rows. – DCJones Nov 12 '21 at 18:13
  • You definitely want a `while` loop, so can you update your posted code with what you've tried? Also, I'm seeing `$row_signage` and `$row`, but I'm not seeing how the latter is set. Lastly, for every `if` block I always recommend having a final `else` branch. If you don't think it will happen, then put a `throw new \Exception('This should never happen');` – Chris Haas Nov 12 '21 at 18:32
  • Hi Chris, Many thanks for your pointers, taken aboard and rewritten the code which now works. See code rewrite. – DCJones Nov 12 '21 at 20:54
  • Please don’t include the solution in your question body - that’s what the “Answers” section below is for. Feel free to self-answer and accept as such. – esqew Nov 12 '21 at 20:55

1 Answers1

1

Code rewrite and working:

<?php while($row = $fb->fetch_array()): 
 $FileType = $row['FileType'];
   if($row['DisplayType'] == "P" ) {
       $DisplayWdith = "1080";
       $DisplayHeight = "1920";
   } else { 
       $DisplayWdith = "1920";
       $DisplayHeight = "1080";   
   }
  ?>
    <div class="banner-container" data-delay-time="<?=$row['TimeLapse']?>">
      <div>
    <a>
    <?php if($FileType == 1) { ?>
      <img src="/<?=$ImagePath . $row['SignageImageName']?>" class="responsive"/>
    <?php } else { ?>
        <video id="video_player" width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
            <source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
        </video>
    <?php } ?>
    </a>
      </div>
    </div>
  <?php endwhile ?>
DCJones
  • 3,121
  • 5
  • 31
  • 53