Need help with a php script idea for images

Discussion in 'PHP' started by Negative, Aug 1, 2007.

  1. #1
    Hello, first post here. I am trying to learn php and I have a small project I need some help with.

    I have a directory on one of my sites and in it are little images (icons, smileys, etc..) I have an index.php page in there as well, which is where I want the "magic" to happen.

    Okay, here is what I am trying to do, in a nut shell...

    1) when browsed to, the index page will display all the images in the directory (have this part done)

    2) the display of the images should be done in rows and columns, I think 5 to 10 columns and as many rows as needed, based on # of images in directory (this is not done)

    3) images need to be clickable links (this is not done either, I will probably be using JS to tell what happens when an image is clicked on, but thats for later anyway)

    What I have so far is this (bear in mind I know very little PHP, so details are important. PLEASE don't just say "add a variable blahblah for this thing there" but instead please help me understand what I am doing :) )

    
    <?php
    
    //  Define  the  full  path  to  your  folder,  shouldn't  need  changing
    $path  =  "./"; 
    
    //  Open  the  folder
    $dir_handle  =  @opendir($path)  or  die("Unable  to  open  folder"); 
    
    //  Loop  through  the  files
    while  (false  !==  ($file  =  readdir($dir_handle)))  { 
    
    //  Prevent  this  file  itself  being  shown
    //  If  you  want  it  to  not  display  any  other  files
    //  enter  more  of  these
    if($file  ==  "index.php") 
    continue; 
    //Prevent  folders  showing
    if($file  ==  ".") 
    continue; 
    if($file  ==  "..") 
    continue; 
    
    //  Display  the  results
    echo  "<table><tr><td><img  src='$file'  alt='$file'><br  /></td></tr></table>"; 
    
    } 
    
    //  Close  it
    closedir($dir_handle); 
    
    ?> 
    
    PHP:
    This is a nice start, but I have 80 some images in each directory, and a single vertical line of images is not helping. Hence #2 above :)

    Any help I can get with this is most appreciated!

    ~Negative.
     
    Negative, Aug 1, 2007 IP
  2. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <table>
    <tr>
    <?

    $path = "./";

    $dir_handle = @opendir($path) or die("Unable to open folder");
    $count=0;
    while (false !== ($file = readdir($dir_handle)))
    {

    if($file == "./")
    continue;
    if($file == ".")
    continue;
    if($file == "..")
    continue;
    if (($count >1)&&($count %5==0))
    echo '<br>';
    ?>
    <td><img src='$file' alt='$file'><br /></td
    <?
    $count=$count+1;
    }
    closedir($dir_handle);

    ?>
    </tr>
    </table>";
     
    nagasharmi, Aug 1, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    <?php
    
    //  Define  the  full  path  to  your  folder,  shouldn't  need  changing
    $path  =  "./";
    
    //  Open  the  folder
    $dir_handle  =  @opendir($path)  or  die("Unable  to  open  folder");
    
    ?>
    
    <table>
    <tr>
    
    <?php
    
    $imagecount = 0;
    
    //  Loop  through  the  files
    while (false  !==  ($file  =  readdir($dir_handle)))
    {
    	//  Prevent  this  file  itself  being  shown
    	//  If  you  want  it  to  not  display  any  other  files
    	//  enter  more  of  these
    	if(!in_array($file, array("./", ".", "..")))
    	{
    		$fullpath = $path . basename($file);
    		
    		//  Display  the  results
    		echo "\t" . '<td><a href="' . $fullpath .'"><img src="' . $fullpath .'" border="0" /></a></td>' . "\n";
    	
    		if (++$imagecount % 5 == 0) // 5 for 5 columns
    		{
    			echo "</tr>\n<tr>\n";
    		}
    	}
    }
    
    //  Close  it
    closedir($dir_handle);
    
    ?>
    </tr>
    </table>
    
    PHP:
     
    nico_swd, Aug 1, 2007 IP
  4. Negative

    Negative Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the help! I now have rows and columns and I know how to adjust them! Awesome!

    Now off to make the click work how I need it to! (insert image name in a path)

    Thanks again!
     
    Negative, Aug 1, 2007 IP