www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Dynamic For Loop from amount of files in folder

  1. #1
    Join Date
    May 2012
    Posts
    10

    resolved [RESOLVED] Dynamic For Loop from amount of files in folder

    I'm trying to create a dynamic for loop that echos all the images in a folder.

    I'm using the query string to switch folders for the images. If you go to http://btseagrave.co.uk/ink/index.ph...&artist=bianca you will see what I mean. On the artists page a file called gallery.inc.php is included which displays the images depending on which artist page its on. Here is the gallery.inc.php so far, I'm not sure why it isn't working. Any suggestions?

    PHP Code:
    <div id="gallery">
    <?php

    $directory 
    "images/$cartist/thumb/";
    if (
    glob($directory "*.jpeg") != false)
    {
        
    $filecount count(glob($directory "*.jpeg"));
    }
    else
    {
        
    $filecount 0;
    }

    $cartist $_GET['artist'];
    for(
    $i=1;$i<=$filecount;$i++) {
        echo 
    "<a href='#'><img src='images/$cartist/thumb/$i.jpeg' alt='$cartist' /></a>";
    }

    ?>
    </div>
    the thumbnail images that I want to display are in images/*artists*/thumb (e.g images/bianca/thumb/1.jpeg & images/bryan/thumb/1.jpeg)

    Also I don't have to stick to this method, if theres a better method please share!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    How about something like this:
    PHP Code:
    $cartist basename($_GET['artist']); // prevent directory traversal attacks
    $directory "images/$cartist/thumb/";
    if(
    file_exists($directory) and is_dir($directory)) {
        
    $files glob($directory "*.jpeg");
        if(
    is_array($files)) {
            foreach(
    $files as $file) {
                echo 
    "<a href='#'><img src='".htmlspecialchars($file)."' alt='".
                     
    htmlspecialchars($cartist)."' /></a>";
            }
        }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2012
    Posts
    10
    That works great, thank you!

    I now have one more problem though:
    I'm trying to use a Lightbox which requires the anchor around the image to link to the actual image not the thumbnail. The anchor needs to link to the same file but in a big folder instead of thumb folder. (e.g. images/bianca/big/1.jpeg instead of images/bianca/thumb/1.jpeg). I would just change the directory variable but I still need to print out the thumbnails on the page.

    Know a solution?

    Thanks!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    How about a second variable for the full-sized directory, then just pop that in where you need it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2012
    Posts
    10
    How would I do that? Because the directory is used in the foreach loop so how would I change it?

  6. #6
    Join Date
    May 2012
    Posts
    10
    Would I have to make another foreach loop?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    Well, assuming you use some sort of consistent naming convention, I might do:
    PHP Code:
    $cartist basename($_GET['artist']); // prevent directory traversal attacks
    $imageDir "images/$cartist";
    $thumbDir "$imageDir/thumbs"
    if(
        
    file_exists($imageDir) and
        
    is_dir($imageDir)      and
        
    file_exists($thumbDir) and
        
    is_dir($thumbDir))
    {
        
    $curDir getcwd();
        
    chdir($imageDir);
        
    $files glob("*.jpeg");
        if(
    is_array($files)) {
            foreach(
    $files as $file) {
                
    printf(
                    
    "<a href='%s/%s'><img src='%s/%s' alt='%s' /></a>\n",
                    
    htmlspecialchars($imageDir),
                    
    htmlspecialchars($file),
                    
    htmlspecialchars($thumbDir),
                    
    htmlspecialchars($file),
                    
    htmlspecialchars($cartist)
                );
            }
        }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles