PHPBuilder - Create An Online Photo Gallery



RSS Twitter
Articles Application Architecture

Create An Online Photo Gallery

by: Brett Patterson
|
November 21, 2005

Making photos.php a PHP Page
We're all set with the appearance of the page (for the most part) but we still need to make it work. We know that we need to have the "class.photogallery.php" script included or it won't work, so let's do that at the very top of the page. Add the line to require that class above the HTML code:
Code:
<?php   require_once('class.photogallery.php'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML>
What About the Images?
We're stepping closer to getting a working gallery. At this point we just need to make a new object from our class (discussed on the next page) and then send the URL information to it, so we use the following php code:
Code:
<?php   $pix = new PhotoGallery();   $pix->get_items($_GET['cat'], $_GET['evt'], $_GET['pic']); ?>
Some Issues
This works great if there is always a value for each $_GET variable. Since most people will come to the gallery and not follow a specific link to a photo, we need to compensate for that potential problem. To do so, we check to see if the $_GET variable is set, and if it is, we set it to its original value; if not, we set it to an empty string:

Code:


$_GET['cat'] = (!isset($_GET['cat']))?'':$_GET['cat'];
$_GET['evt'] = (!isset($_GET['evt']))?'':$_GET['evt'];
$_GET['pic'] = (!isset($_GET['pic']))?'':$_GET['pic'];

The ternary operator usage here is for ease of use. It works like this:

Code:


$var = ($expression)?$true:$false;

The value of "$var" will be set to either the value of "$true" or "$false" depending upon whether the value of "$expression" is true or false. It could also be written as:

Code:


if($expression === TRUE){ $var = $true; }

else{ $var = $false }

We've checked our $_GET variables and made sure that they are set (either to an empty string, or their real value) and we need to take those lines and put them just above our object instantiation line:

Code:


<?php
  $_GET['cat'] = (!isset($_GET['cat']))?'':$_GET['cat'];
  $_GET['evt'] = (!isset($_GET['evt']))?'':$_GET['evt'];
  $_GET['pic'] = (!isset($_GET['pic']))?'':$_GET['pic'];

  $pix = new PhotoGallery();
  $pix->get_items($_GET['cat'], $_GET['evt'], $_GET['pic']);
?>

The code that now lies between the <table> tags should look like the following:

Code:


<table>
  <?php
    $_GET['cat'] = (!isset($_GET['cat']))?'':$_GET['cat'];
    $_GET['evt'] = (!isset($_GET['evt']))?'':$_GET['evt'];
    $_GET['pic'] = (!isset($_GET['pic']))?'':$_GET['pic'];

    $pix = new PhotoGallery();
    $pix->get_items($_GET['cat'], $_GET['evt'], $_GET['pic']);
  ?>
</table>

The entire "photos.php" should look like:

Code For: photos.php


<?php
require_once('class.photogallery.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> PHP Image Gallery </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Brett Patterson">
<META NAME="Keywords" CONTENT="PHP Image Gallery PHPBuilder Builder">
<META NAME="Description" CONTENT="A simple PHP Builder Article overviewing an Image Gallery">
<link type="text/css" rel="stylesheet" href="phpbstyle.css">
</HEAD>

<BODY>
<div id="wrap">
  <div id="header">
    <img src="images/site/phpb_logo.gif">
    <span class="tag">Image Gallery for PHP Builder</span>
  </div>
  <div id="main">
    <p>
      <table>
        <?php
        // Set defaults for the $_GET array
        $_GET['cat'] = (!isset($_GET['cat']))?'':$_GET['cat'];
        $_GET['evt'] = (!isset($_GET['evt']))?'':$_GET['evt'];
        $_GET['pic'] = (!isset($_GET['pic']))?'':$_GET['pic'];

        $pix = new PhotoGallery();
        $pix->get_items($_GET['cat'], $_GET['evt'], $_GET['pic']);
        ?>
      </table>
    </p>
  </div>
  <div id="clear"></div>
</div>
</BODY>
</HTML>

Let's get our class working so we can view some photos!!


1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Brett Patterson

Comment:



Comment:

(Maximum characters: 1200). You have characters left.