topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday March 19, 2024, 6:35 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

questorfla

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


<?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
    • View Profile
    • Donate to Member
Re: Adding a linefeed Before jpg images in a display window
« Reply #1 on: February 09, 2017, 04: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.

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