by Glenn Hefley
HTML is the language of web pages. This Hyper Text Markup Language is what your browser (Microsoft Explorer, Firefox, Netscape and all the others) understands. Most good browsers, and certainly any browser worth using, also understand and can process Javascript, and CSS (Cascading Style Sheets).
What this means is that these commands are processed on your computer. Your browser, and these languages, can not process any further information on the server where the web site is, so dynamic information from databases is not readily possible. What you are looking at right now, this article, has already been downloaded to your computer, and you are reading it from a temporary directory.
PHP allows for processing information from a database, and building a dynamic HTML page while the page is still on the server, prior to the download to the visitor's computer. Let's take a simple example. The shopping cart. 
Just about everyone knows what a web shopping cart is. The items we are looking to buy are displayed on the page. We click a button to add items to our cart so that we can buy them. If the web site wishes to use a data base, then the program of the web site is required to have some type of program running on the web hosting server to add that item to the data table. This is where PHP comes in.
PHP is not the only language which allows dynamic web site construction. Perl, ASP and Cold Fusion are also fully capable of performing the same actions. Some even claim that Perl is faster at the average tasks. So why PHP?
PHP has a few advantages over the other alternatives. For the most part, it is much faster at processing commands than ASP or Cold Fusion. PHP doesn't require a Microsoft server, though it can run on one. PHP works with just about any database (MySQL, Oracle, Informix, DB2, MSQL). PHP is more commonly found in the offerings of web hosting services.
All of these points are very important to us if we want to develop a web site that delivers dynamic information. The expenditure of time, and money involved is considerable. On top of this, web hosting providers are notorious for going out of business at exactly the wrong time. If we need to move our site quickly, we want our site to work on the widest possible collection of available hosting companies.
Another advantage to using and learning PHP is that you don't have to know much to get some good mileage out of the language. On top of this, small bits of PHP can be put straight into our HTML pages, which makes it even easier to use, and allows for some very creative displays.
Let's take a really simple example, which can be very effective for web sites. Our goal is to display a different picture on our page every time a visitor opens or refreshes the browser. A common reason is to keep the page looking fresh, and to introduce as many products to the visitors as possible. We have eight products we wish to show randomly to the visitor.
We might at a later date also wish to add images, or remove the ones we are showing currently.
The first thing we want to do is to insure that our web hosting company is running PHP and that it is available. So we will use this very simple script to check that out.
Create a web page file called index.php. The .php extension is required on most hosting servers. Some are setup to process PHP from inside .html files as well. You can check that out next simply by changing the file name to index.html after you are done with this first test. In the text file put this code:
<html>
<head>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>
On your web server create a directory called test. Now, save the file we just created, and go to http://www.yourwebsitename.com/test/ and see what comes up. What you should see is the PHP logo, and some very detailed information about your web server's PHP installation. The phpinfo() function was created for exactly this purpose. Very handy to have, and certainly saves a great deal of time trying to figure out where things are, and what is going on as you begin to use PHP for more robust projects.
Okay we are ready to build up our dynamic image display. First we need 8 pictures of the same size, so that when they change, they don't mess up our HTML format. Once we have those we will put them in a directory called images under our test directory. Once you get everything working you can move them out into the real world of your web site. Right now, lets stay inside the test directory we created.
Now, so that we can edit quickly and not mess up our PHP code later (and also so I can show you how to read text files off the server) we will make a text file with each of the file names, one per line. Like this:
modding1.jpg
modding2.jpg
modding3.jpg
modding4.jpg
modding5.jpg
modding6.jpg
modding7.jpg
modding8.jpg
We will save that text file and call it images.inc. Now, the reason I called this file with a .inc extension is I didn't want it showing up in search engine displays. Don't use the .txt extension for your data files or inclusion files. You would be amazed at how many people do this with files like passwords.txt. Don't take my word for it, go to Google and search for password.txt filetype:txt
http://www.google.com/search?sourceid=navclient-ff&ie=UTF-8&rlz=1B2GGGL_enUS176&q=password.txt+filetype%3Atx t
While hackers are part of the landscape in the cyber world, lets not give them cart blanche.
Okay, we have our images in a directory, and we have our image list in images.inc. We load that to our test directory as well. Let's go back to our index.php file and do some editing. We are going to remove the phpinfo() function and put in some code. First we want to read the file into an array, so that each cell in the array has one file name. An array is like a row of cells in a spread sheet, or a row of pill boxes connected together. In PHP all of our variable, regular variables, arrays, and many others start out with the dollar sign $. We will create an array, load the array from the file. The randomly choose a name from that file, and then display the image of that name.
So our code will be this :
<html>
<head>
</head>
<body>
<img src="<?php
$images = array();
$images = file('images.inc');
$i = rand(0,7);
$rimage = $images[$i];
print "images/".$rimage; ?>">
</body>
</html>
You can see the example for this one my web site (here), and also download the working example to try out on your web server as well. Now that you have this and can see it working, let's walk through what we got here.
Our code starts out with the bare necessities for an HTML document. Nothing in the HEADER area, but there could be. Just because I don't use it here, doesn't mean that you can't use HTML to its full effect, with complete headers, JavaScript and everything else.
After the body tag I start the IMG tag <img src=" With the first quote symbol there, and then the <?PHP to let the server know that processing needs to be done to find this name. So before the web server sends this file, it will process the PHP inside this tag, and any other PHP tags you have in this file. To start PHP you put in the tag <?PHP and to end the PHP area you use ?>
<?PHP ... your code ... ?>
Once PHP is started, we can get down to business. The first line of my code, creates a variable named '$images' and then sets it to be an empty array. This is left-overs from my paranoid days as a Pascal and C++ programmer. It is called 'declaring' and while not technically necessary, it is a good habit to be in. As you learn more you'll probably run into it being discussed. For now, what that does is tell PHP that I want this variable to be an array, which is empty.
The next line asks PHP to read a file named images.inc and to put each line of that file into my array named $images. So file('images.inc') reads the text file from the current directory (the same directory we are in), one line at a time. Then for each line it puts it into a cell of my array.
Each cell of my array is named with a number, starting with 0 (zero). Notice I'm not telling PHP how big the array is or any of that tedious other stuff usally required by a programming language. I only had to tell PHP that I want an array and this is what I want inside. It handles the rest. This is another very nice thing about PHP, and something many programmers have come to love about the language. It is very easy to work with, with very few tedious areas.
Now, in the future I don't want to have to change this code, I want it to work no matter how many image names I have in the images.inc file. Whether that is 2 or 200. In order to get a random number which is meaningful to our purpose however, I need to give the function rand() two numbers, a low and a high. It can then give me a random number between those two. I want this for the array we just made. The number has to be equivalent to a cell in the $images array which has a name inside. Right now we know there are eight names in the list, so I could give the function rand() the numbers 0 and 7. But I don't want to hard code it like that, for reasons we already stated.
To get the number of the last cell in an array with a value inside the cell, you use the function count(). count() as the name implies, counts how many cells are there. In this case the number will come back 8. The code I have on that line, places the value given to me by count() into the variable $c. $c came to life on this line and is given a value, in this case 8.
Eight is not a value I can use, arrays begin their names at zero, so I need to lower whatever number I get from count by 1. So my next line is $c = $c - 1; Minus one from whatever value is inside the variable $c. No worries.
Now $i, who is birthed on the next line, is going to have the value given by rand() now. $i = rand(0, $c); Pick a number between 0 and 7 and give it to me in the variable $i.
My next variable is called $rimage, for random image. This variable on the next line is given the name from the cell number in $images using the [] brackets. Whatever number is in $i at the time is the a cell with a name, and that name is copied to $rimage now.
Finally I'm ready to print out to the browser my image name. I use the print() function to do this, which as the name suggests prints things. The first thing I print is inside quotations. "images/" this is the directory name where we put our image files. The next is a period, which is a quick way to say "Plus" or "And" if we were to sound this line out. The period puts two strings together. In this case the directory name, plus whatever name is inside that $rimage variable right now.
Then I end the PHP area with ?> and I am done. The rest of the code is all HTML.
While this did take quite a while to explain, you can see that we are able to do quite a bit with very little code. In many other languages it would not be this simple, and we could not simply embed our code into the HTML.
There are many simple lines we can put into our HTML which will enhance our pages as well. How about putting the date at the Top of the page.
<?PHP echo date('l dS \of F Y h:i:s A'); ?>
This puts something like: Monday 15th of August 2005 03:12:46 PM in there for us.
You could use the same code above, to put quotes on the page instead of the images, just put quotes in an include file (quote.inc) One quote per line.
What is a "free" gift ? Aren’t all gifts free?
Consciousness: that annoying time between naps.
I don’t suffer from insanity. I enjoy every minute of it.
Better to understand a little than to misunderstand a lot.
The gene pool could use a little chlorine.
Give me ambiguity or give me something else.
Always remember you’re unique, just like everyone else
With the same code shown above we can replace whole sections of our web page. Perhaps "today's menu" for restaurants who change the menu each day of the week. Now we are just reading a file with HTML code (probably a table) and printing it using the print() function. The code will also need a FOREACH loop to go through the array, because just printing the array will not give you what is inside. So the code would look something like this:
<?PHP
$menu = file("menu.inc");
foreach($menu as $mline)
{
print $mline;
}
?>
From this point, we already saw above the use of the DATE function. We also saw that it has the ability to print the name of the day of the week. So if our restaurant did have different soups of the day or menu selections for each day of the week, we could have seven include files with those different menus inside, in HTML format, and have PHP show the correct one. This might look something like :
<?PHP
$dayfile = date(l).".inc";
$menu = file($dayfile);
foreach($menu as $mline)
{
print $mline;
}
?>
Again, the nice thing about PHP scripting is that you don't have to know a great deal about it to get some nice effects on your web pages from its use. And as you learn more about the language, as you add to your vocabulary, you can build on the ideas you have already developed in surprising and wondrous ways.
This article may not be copied or distributed in part or in full from this site and is copyright D24 Media Limited.
|