ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Adding a linefeed Before jpg images in a display window


The following code is part of an HTML file used to display images. There are several lines before his but i cut to the chase with the only working part I need help with.  I need to get it to add a line feed <br> only before it displays a .jpg file.

The contents of the folder are all png image files except for the index images which are jpg.  I am trying to get them to show up with all the png under the index they belong to.
If working properly, it should start by adding a line feed before displaying the first index file named "A.jpg", then display A.jpg followed by all the the A***.png files, then add a linefeed before displaying the "B.jpg index" followed by the B***.png files.

I am pretty sure that this needs to happen in the last 4 lines of code but I cannot seem to get the <br> to work dependent on the type of file.
 I need to add a <br> to the front of the display only if the file-type is a jpg.
As it is now, it displays the files end to end with the title indexes being followed by their png's then the next index file with no break. 
This works as is but I would like to try getting it a little better organized for display purposes.

--- ---<?php
$folder_path = 'images/'; //image's folder path
$num_files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);
$folder = opendir($folder_path);
 if($num_files > 0)
 while(false !== ($file = readdir($folder))) 
  $file_path = $folder_path.$file;
  $extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION)); 
    if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp') 
     $txtfile_temp = basename($file_path, ".".$extension).PHP_EOL;
  $txtfile = $folder_path.trim($txtfile_temp).".txt";
     $fp = fopen($txtfile, "r");
      if ($fp == false)
   $file_path = "Failed to open ".$txtfile;
   $line = "Failed to open ".$txtfile;
       $line = fgets($fp);
            <a href="<?php echo "".$line; ?>"><img src="<?php echo $file_path; ?>"  height="300" /></a>

I haven't done any PHP.  So I am just taking a shot in the dark.  But my idea would be after the line where you check it is one of the desired graphics formats, then use a ternary operator to test just for .jpg and if so, preload the $line variable with a br.

--- ---if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
     $line = ($extension=='.jpg') ? "<br>" : ""
     yadda yadda

Then later instead of assigning to $line, append to it, so that the br will lead if it is already in the $line variable.  I am just guessing that will insert the br properly in the anchor tag.  But that's all it is, a guess.  :)


[0] Message Index

Go to full version