Llamasery.com Home  
 
Server-side Starter Tutorial: Text Files - Part 3 of 4
Alfie the alpha_geek: Llamasery guru-in-waiting Tutorials Purpose

Starter Tutorials

Tell-a-Friend
Tell-a-Friend +
The 'Contact Form'
php Includes
Simple Themes
MySQL database 101
Comments System
Text Files

Intermediate Material

Articles/Tutorials

Text files for data

The visit counter uses just one piece of information from a text file. Other applications similar to the visit counter could append visitor IP address, browser type, etc. to a text file used as a visitor log. If you look around various Internet php script sites, you'll be sure to find good examples of that sort of script (unfortunately, you'll also find bad examples - but that's the way of the Interent).

Our second example application of text files is slightly different. We'll read information sequentially from the text file and use that information together with a script to display the information. A simple example of that might be displaying the 'latest' news on your site. No need to re-code your page every time there's a fresh piece of news; no need to fire up your server database administration tool whenever you want to update the news; just edit a text file and upload it to your server.

This is exactly the method used at CommemorateWTC.com for diplaying the Countries List, where the data for country names, number of victims, and whether CWTC has been visited from those countries is all drawn from a simple text file and then a twenty-line php script displays all of the information.

Our example isn't going to as complex as that, but will give you a good idea of how it can be done. The 'secret' is that the text file contents are read into an array, and then we step through the array to grab each element. The element is 'formatted' using simple html commands and added to a text string until the entire array has been read. Then the text string is displayed with php's echo command.

Our example is going to be a small table that shows the price of three 'specials of the week' ... and it looks like this:

Meat Pies $12.00 per dozen
Strawberry Ice Cream $4.99 per quart
Fine Wine $1.99 bucket

And the text file that is used as our 'database' looks like this. When you're ready for the code, click the 'next' link below.

«  previous  |  next  »