<<Return to Week | 1 | 2 | 3 | 4 | 5

PHP Project Development Example

On these pages, I will step you through the development process I followed in coming up with a set of PHP pages that 1)stores urls for image files in a MySQL database table and 2)selects a random record from this database every 5 seconds, and displays the image file. The finished page is here. There are two PHP files of code that are used in this project. The first, "loadimages_1.php" loads the urls into the database, and the second, "displayimages_1.php" retrieves the information from the database and displays it. The code for these two pages is listed on page 4 and page 5 of this intro.

Planning

The first step is probably the most important-- that is, to decide what you want your page to do. With enough work, PHP can do pretty much anything you want it to do, but it needs highly specific instructions. The clearer an idea you have of your objective, the easier it will be to coax PHP to do what you want. Once you have an idea of what you want to do, it is a good idea to scratch out some "pseudo-code" which identifies the steps needed.

In this example, my idea is to have a web page that shows a rotating gallery of images randomly selected from a pool of available images. So the first thing I do is sketch out a plan. I normally do this on paper with lines and arrows and things scratched out and added. I have included an outline here. I do not start writing code until I have planned the project well enough that I know the major steps involved.

Preliminary work (before getting to the display page)
   1. Create a table in MySQL to hold the URL of image files
   2. Create a folder on the server containing image files
   PHP 
      3. get a directory listing of the files
      4. form the filename into a url
      5. store the url to the MySQL table
Display Page
   PHP
     6. Randomly select a record from the table of image URLs
     7. build an image tag with this url
      (note img tag needs url and size)
   HTML
     8. display the image tag
     9. set autoupdate increment to select next random image

When I have gotten it to this point, I feel pretty comfortable about jumping in and starting to code.

1. Making the Table

I first log into the database using phpMyAdmin (http://intermedia3.art.uiowa.edu/phpmyadmin/) and create a table named mn_example2.

This table has 2 fields: "key" and "url." The "key" field is a unique number known to the database as a primary key.(notice the "auto_increment" attribute in the "extra" column and the primary key icon checked).

2. Create a folder of image files

I establish a folder on the server named MyPHP where I will store my PHP files, and inside this I put a folder named "images." I then transfer all of the images I want to use into this folder. The directory structure I have chosen to use is not required. You can use any structure that you come up with. But in setting it up this way I have done several things that will make my life easier down the line: the folder is easily accessible with a relative link from the location of my PHP files, there are no spaces in the names, and I have used lower case in all subfolders that I will be accessing with PHP (remember that PHP is case-sensitive, so "images" and "Images" are two different folders).

<<Return to Week | 1 | 2 | 3 | 4 | 5