Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple image gallery without changing HTML

Options
  • 05-04-2011 12:01pm
    #1
    Registered Users Posts: 2,985 ✭✭✭


    What would be the best way to approach this?

    Basically what I want to do is have a couple of image galleries on several pages, each one obviously displaying different categories of content. I want the gallery to update itself when I add an image to a folder without me having to go in and edit the HTML. The galleries will all have exactly the same format with the only variable's (apart from alt & title tags) being the file name and folder the image is located in.

    I started looking into PHP a few days ago and I came up with this (bear in mind I'm a complete novice)....
    <?php
                                    $imageNumber = "1";
                                    $thumbNumber = "1";
        
                                    $directory = "_images/" . $folder . "/";
            
                                    $fileLimit = count(glob("$directory*.jpg"));
                                    
                                
                                $fileCount = 0;
                                while ($fileCount < $fileLimit){
                                $newImage = "<li><a class=\"thumb\" name=\"" . $imageTitle . "\" href=\"_images/" . $folder . "/" . $imageNumber++ . ".jpg\"" . " title=\"" . $imageTitle . "\"><img src=\"_images/" . $folder . "/_thumbs/" . $thumbNumber++ . ".jpg\" alt=\"" . $imageTitle . "\" /></a></li>";
                                echo $newImage;
                                $fileCount++;
                                }
                                
                                ?>
    
    What that does for me is go into the specified folder, count the number of files and then echo's them back in HTML adding 1 to the .jpg file every time. (I have all the images in each folder numbered from 1 up).

    That works fine as long as all the images are numbered in order, it gets annoying if want to remove any of the images.

    In an ideal world I would have a little function that would go into a folder and echo back an image for every file it finds with a .jpg extension, though I don't know if that's even possible. I'm not asking for anyone to write the code for me, just point me in the right direction with regards to what function I should be using or if PHP even is the right language to do this with.

    This request (as well as the PHP code above) may seem pretty pointless to most of you but really I just want to know how to do things like this for future reference as I'm currently learning and putting together a portfolio of sites.
    Thanks.


Comments

  • Closed Accounts Posts: 5,824 ✭✭✭RoyalMarine


    ive just finished making an image upload / gallery website for college.

    i used php and mysql to create user accounts and the user can upload images to their folder.
    once uploaded, the files are given a new name based on the time and date it was uploaded.
    only .jpg files are allowed. everything else is rejected.

    i then use php function to show the images using a javascript style on the website.

    if you wanna have a look, pm me your e-mail address and i can send you on all the files.

    i dont use it anymore as i submitted it today and finished with it now.

    just to note, you need to have xampp with php and my sql installed. (only takes 5 minutes to set up)

    the code is very easy to understand and it has a lot of validation so only the user can view their images etc.


  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    Just a note, when uploading and renaming files, it's a good idea to do date and a random string, just in case 2 are uploaded at the same time!

    Any time I've made a gallery, I store urls in the DB, output the links with PHP and then use javascript to display them nicely!


  • Closed Accounts Posts: 5,824 ✭✭✭RoyalMarine


    the scripts i sent him upload images and rename them with a date time stamp and random string. very handy rather than letting the user choose the name for the file.


Advertisement