Building Dynamic Web Sites:Part IV
Part: | I | II | III | IV | V |
---|
By Chris Wenham
Summary: Learn how to create a web catalog by importing your information from a database or spreadsheet program.
Last month I showed you how to create a simple "poor man's database" that was used to generate a catalog web site, one that could be used in an e-commerce site or "Web storefront." The database wasn't really a very practical one, as its "format" was very ad-hoc and consisted mostly of embedding commands for the HTML preprocessor into the data itself. Not very elegant, and not very editable. If you had a catalog with hundreds of items, the database file could get extremely unwieldy in an ordinary text editor. What we really need is the ability to import from a real database.
Luckily this is easier than it may seem. Most database programs for OS/2 can export to a comma or tab delimited format and those are a piece of cake to work with. In fact, PPWizard has an #import function that does just this.
#import
This function is one of the most complex and powerful in PPWizard's set. By default, the command will read a comma delimited, tab delimited and fixed-column-width file and format the contents it finds into a table - the most obvious structure for what is essentially data in tabular form. For example, say you had a comma delimited file called staff.csv that looked a bit like this:
Chris Wenham,Editor-In-Chief,chris@os2ezine.com Chris Wright,Senior Editor,wright@dtcweb.com
And imported it into a web page with this line:
#import staff.csv CMA "Name" "Position" "Email"
Then it would generate the code for a table that looks like this:
Name Position Email Chris Wenham Editor-In-Chief chris@os2ezine.com Chris Wright Senior Editor wright@dtcweb.com
It's quite simple to see how PPWizard mapped the names of the fields with the records in the staff.csv by taking them in the order in which they were named on the #import line. As it turns out, you have the power to rearrange the order of these columns as you please, but we won't be covering that this week.
Looking at the #import line itself, you see that after the filename of the file to import there's a keyword "CMA." This tells PPWizard that it's importing a CoMmA separated file. Other keywords are "TAB" for tab delimited files (good if some of your records have commas in their data) and "FIX" for fixed width files. Check the PPWizard manual, because additional format support may have been added by the time you read this. There's also the option to specify the delimiter yourself, so if you used semicolons instead of commas, PPWizard will know what to look for.
If you put a minus sign after the file type keyword, such as "CMA-" or "TAB-" then PPWizard will ignore the first line it finds in the file - assuming it's probably where the database had inserted the names of the fields rather than the first actuall record.
It's easy to change the way the table looks, because PPWizard stores its defaults in variables you can override with the #define command. For example, say we put these two lines before the #import statement:
#define IMPORT_TABLE_ATTRIBS BORDER=1 CELLSPACING=2 #define IMPORT_RECORD <TR><TD ALIGN=CENTER>{$Column1} \ <TD ALIGN=CENTER>{$Column2} \ <TD ALIGN=CENTER><a href="mailto:{$Column3}">{$Column3}</a></TR>
(The backslashes "\" tell the preprocessor that the #define statement is continued on the next line)
PPWizard would then generate a table that looks like this:
Name Position Email Chris Wenham Editor-In-Chief chris@os2ezine.com Chris Wright Senior Editor wright@dtcweb.com
Not only was the border and cell spacing changed, but the e-mail addresses are now properly hyperlinked. We just told PPWizard to format records differently than the default settings specify. There are several other defaults that can be overridden, ones that can switch off the formation of tables altogether. It's these that we're going to use when creating a better version of the "Catalog Maker" from last month.
Making #import act like a conversion utility
We could settle for making our web catalog take the form of a boring table, but we're not. Remember that makeshift "poor man's database" format from last month? It's actually pretty good for making PPWizard create all the files for us, but it really only should have been an intermediate format, a temporary work file that gets thrown away. What we need to do is convert the easily manageable and exportable CSV format into our "poor man's database" format and keep the same templates from last month.
To do this we're going to make the preprocessor eat its own lunch, in effect. First we make it create a poor man's database, then we make it use it. Take a look at the following code, stored in a file called convert.it, and you'll see that we're tricking PPWizard into writing "#define" statements instead of finished HTML code. We'll then make it read back those #define statements that it just generated, using them to build our catalog pages.
;Lets create and start writing to a new file #output database.it AsIs ;Clear all the defauts used to define a table's HTML code #define IMPORT_BEFORE #define IMPORT_AFTER #define IMPORT_HEADER ;Create "#define" statements for each record #define IMPORT_RECORD <?hash>define+ File {$Column1}<?NewLine> \ <?hash>define+ Name {$Column2}<?NewLine> \ <?hash>define+ Materials {$Column3}<?NewLine> \ <?hash>define+ Description {$Column4}<?NewLine> \ <?hash>define+ Picture {$Column5}<?NewLine> \ <?hash>define+ Price {$Column6}<?NewLine> \ <?hash>include item.it<?NewLine> ;Import the tab delimited file with the database contents #import catalog.csv CMA '' "File" "Name" "Mat" "Desc" "Pic" "Price" ;Close the file #output
It assumes there's a file called catalog.csv, looking a bit like the "staff.csv" of earlier, but which contains our catalog of goods instead. Notice the "<?hash>" and "<?NewLine>" tags. These are built-in to the preprocessor and are substituted with the hash character ("#") and a carriage return respectfully. They had to be referenced this way so that PPWizard wouldn't try to interpret the line before we're ready.
The IMPORT_BEFORE variable used to contain the "