DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Adding a linefeed Before jpg images in a display window  (Read 4,409 times)

questorfla

  • Fighting Slime all the Time
  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
Adding a linefeed Before jpg images in a display window
« on: February 09, 2017, 08:53 PM »
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.


Code
<?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;
	  }
      else
      {
      	$line = fgets($fp);
      	fclose($fp);	  	
	  }
   
?>
            <a href="<?php echo "".$line; ?>"><img src="<?php echo $file_path; ?>"  height="300" /></a>
            <?php
         }

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: Adding a linefeed Before jpg images in a display window
« Reply #1 on: February 09, 2017, 10:11 PM »
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.

Code
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.  :)


Original address: https://www.donationcoder.com/forum/index.php?topic=43504.0