#native_company# #native_desc#
#native_cta#

Create An Online Photo Gallery

By Brett Patterson
on November 21, 2005

Into the Class
Thus far we’ve got a pretty solid base to stand on. We have a layout, and we have checked to make sure that the $_GET items that are passed to the class are proper (i.e. either set or an empty string). We’ve also set the name of our class and its first function. The name of our class will be “PhotoGallery”. We already know this because it’s what we put in our “photos.php” script. Let’s start off with a standard class declaration:

Code For: class.photogallery.php

<?php

class PhotoGallery
{
}

?>

Here we define our class. We have our first function (that we know of) called “get_items()” which has all the $_GET variables sent to it. You can think of the function get_items as the stepping stone for the rest of the class. Everything will always start there. In the class, we define the function as we normally would:

Code:

<?php

class PhotoGallery
{
  function get_items($cat=”, $evt=”, $pic=”)
  {
  }
}

?>

We’ve got our function in the PhotoGallery class. Before we move any further, we need to set some things ready. We need to define some variables that will be used throughout the class. If we define a variable inside a function, then it is local to that function, and you can’t use it outside of the function (unless you globalize it). So instead, we’re going to make our own global class variables that can be used anywhere in the class at any point in time. Let’s think about what information we need, and what we need to store:

    * Root Photo Directory
    * Root Icon Directory
    * Array of all our files
    * Any output we have
    * Any error messages
    * Whether we want to run in Debug Mode
    * Number of photos (columns) across before we start a new row

Simple enough. We just need to declare these variables, and they are easily declared as like so:

Code:

<?php

class PhotoGallery
{
  ###############
  ## Varaibles ##
  ###############
  var $_rt = ‘images/photos/’; // Where the base “root” of our photos are
  var $ico = ‘images/icons/’; // Where our icons will be held
  var $lst = array(); // Will hold our list of files (or directories)
  var $out = ”; // What will be outputted/displayed to the user
  var $err = ”; // Holds errors that are encountered along the way
  var $dbg = FALSE; // Defines whether debug mode is “on” (TRUE) or “off” (FALSE)
  var $cols = 5; // How many columns there will be across

  ###############
  ## Functions ##
  ###############
  function get_items($cat, $evt, $pic)
  {
  }
}

?>

We have some class global variables and we can reference them and their values at any point in time anywhere in the class. We do this by using the following syntax:

Code:

$this->variable

If I wanted to echo the root photo directory ($_rt) I would do so in this way:

$this->_rt
The “$this->” operator defines that the variable has a global scope from within the current class. This is just a part of Object Oriented Programming. Any time you reference any of the variables listed under the Variables heading at the top, it needs to have “$this->” in place of the normal “$”.
So the fun part over–let’s get into this “get_items” function. Basically, we want to loop through a directory and return all the files or directories there are to choose from. If each of the three variables are empty, then we want to loop through the root photo directory to get the categories. If the $cat variable is populated, then we want to loop through that directory to get each event. If both the $cat and $evt variables are populated, then we need to loop through to find all the images to display. And finally if all three variables are populated, then we need to retrieve the picture that is specified and display that.
It sounds like a lot, but once the first one is done, it’s just repititious. Here’s the code used to loop through a directory:

Code:

// Display all categories to choose from
$path = $this->_rt;
$d = @dir($path);
if(!$d){
  // If we can’t open the directory, let’s tell the user
  // something is wrong.
  $this->throw_error(‘Fatal Error’, ‘The directory
[ <i><span style=”color: #999;”>’.$path.'</span></i> ]
could not be opened.  Please check the path and try again.’,
$php_errormsg, $this->dbg);
}
else{
  while(FALSE !== ($f = $d->read())){
    // While we are reading documents from within the directory
    if($f != ‘.’ && $f != ‘..’){
      // If the currently read file is not “this directory” or “Up One Level”
      if(is_dir($path.$f)){
        // Since we’re dealing with categories, we only want directories
        // since the directory name will be categories
        $this->lst[] = $f;
      }
    }
  }
  $this->gen_crumbs();
  $this->gen_page();
}

There are a couple of things you may notice: we added a few new functions, and we put a list of all directories into the “lst” global class variable. We are using the “dir” object to read a directory for its contents. All of the loops are the same with two minor differences. In the thumbnail loop (when $cat & $evt are populated) we look for files (not directories), and in the display pic loop (when all three variables are populated) we just want a loop for a reference point, so we don’t need to generate the thumbnails. Since they’re all so close in code, I’ll give them to you.

They are housed with a few IF() ELSE() statements to find out whether we need to look for directories or images. It’s pretty easy to understand, and I’ve documented it as well:
Code For: get_items()

function get_items($cat = ”, $evt = ”, $pic = ”){
  // Gets all of our items in our folder
  if($pic == ”){
    if($evt == ”){
      if($cat == ”){
        // Display all categories to choose from
        $path = $this->_rt;
        $d = @dir($path);
        if(!$d){
          // If we can’t open the directory, let’s tell the user
          // something is wrong.
          $this->throw_error(‘Fatal Error’, ‘The directory
[ <i><span style=”color: #999;”>’.$path.'</span></i> ]
could not be opened.  Please check the path and try again.’,
$php_errormsg, $this->dbg);
        }
        else{
          while(FALSE !== ($f = $d->read())){
            // While we are reading documents from within the directory
            if($f != ‘.’ && $f != ‘..’){
              // If the currently read file is not “this directory” or “Up One Level”
              if(is_dir($path.$f)){
                // Since we’re dealing with categories, we only want directories
                // since the directory name will be categories
                $this->lst[] = $f;
              }
            }
          }
          $this->gen_crumbs();
          $this->gen_page();
        }
      }
      else{
        // Display all events to choose from
        $path = $this->_rt.$cat.’/’;
        $d = @dir($path);
        if(!$d){
          $this->throw_error(‘Fatal Error’, ‘The directory
[<i><span style=”color: #999;”>’.$path.'</span></i> ]
could not be opened.  Please check the path and try again.’,
$php_errormsg, $this->dbg);
        }
        else{
          while(FALSE !== ($f = $d->read())){
            if($f != ‘.’ && $f != ‘..’){
              if(is_dir($path.$f)){
                $this->lst[] = $f;
              }
            }
          }
          $this->gen_crumbs($cat);
          $this->gen_page($cat);
        }
      }
    }
    else{
      // Display all thumbs to choose from
      $path = $this->_rt.$cat.’/’.$evt.’/’;
      $d = @dir($path);
      if(!$d){
        $this->throw_error(‘Fatal Error’, ‘The directory
[ <i><span style=”color: #999;”>’.$path.'</span></i> ]
could not be opened.  Please check the path and try again.’,
$php_errormsg, $this->dbg);
      }
      else{
        while(FALSE !== ($f = $d->read())){
          if($f != ‘.’ && $f != ‘..’ && !is_dir($path.$f)){
            if($this->check_img($path.$f)===TRUE){
              $this->lst[] = $f;
            }
          }
        }
        $this->gen_crumbs($cat, $evt);
        $this->gen_thumbs($cat, $evt);
      }
    }
  }
  else{
    // Generate a list of files within the directory
    $path = $this->_rt.$cat.’/’.$evt.’/’;
    $d = @dir($path);
    while(FALSE !== ($f = $d->read())){
      if($f != ‘.’ && $f != ‘..’ && !is_dir($path.$f)){
        // This time, we’re only getting all the image files from within our path.
        // This is to help us create the “next” and “previous” links.
        if($this->check_img($path.$f)===TRUE){
          $this->lst[] = $f;
        }
      }
    }
    $this->gen_crumbs($cat, $evt, $pic);
    // Get the proper image to display
    $this->gen_pic($cat, $evt, $pic);
  }
}

Now we can move on to all the other functions we need!! Be sure to come back next week for the rest of this tutorial!

1
|
2
|
3
|
4