PHP Tutorial for a Simple Gallery

Using the PHP language, you can create a simple gallery of images to display to your website visitors. The gallery is a useful technique when you have multiple images that you wish to display, and you want users to be able to click through the images without having to open them on separate pages. To display the gallery on your Web page using PHP, you simply import your images and loop through the sequence.

Instructions

    • 1

      Right-click the PHP page that you want to edit and select "Open With" from the context menu. Click your preferred PHP editor to load and open up the code.

    • 2

      Create two variables that point to the thumbnail gallery images you want to display to your reader. The second variable points to a larger version that displays when the user clicks the image. The following code creates these two variables:

      $images = "image_gallery/";
      $big = "big/";

      Replace "image_gallery" and "big" with your own folder names.

    • 3

      Load each image programmatically into an array variable. The array gives you the ability to loop through the images and create your gallery. This example code loops through the images in the directory:

      if ($file= opendir($images)) {
      while (false !== ($image= readdir($file))) {
      if ($image!= "." && $image != ".." && $image != rtrim($big,"/")) {
      $gallery[] = $image;
      }
      }
      closedir($file);
      }

    • 4

      Loop through and display each image using the following code, which creates the gallery on your Web page:

      foreach($gallery as $file)
      {
      if($colCtr %$cols == 0)
      echo $images . $big . $file;
      $colCtr++;
      }

Related Searches

References

Related Ads

Check It Out

Prep for Black Friday Checklist [Infographic]