Interchange and PHPShop both make a big whoopdy-do about the "flypage." What is a flypage? It's a page about a specific item. If you want to include a picture of an item or a more lengthy description -- perhaps a book review, in the case of a bookstore -- this is the place to do it.
I'm taking the lazy way out -- I'm just taking the result for one book and spreading it over two or three lines, instead of the single-line format I used on the browse page.
Note how we push the user back to main.php if they come here without a valid SKU. Hardly worth a comment, eh?
<?
require_once("startme.inc");
if (! isset($_SESSION["xvalid"])) {
header("Location: main.php");
exit;
}
if (! $sku) {
header("Location: main.php");
exit;
}
$sql = "select title,author,isbn,publisher,format,`condition`,sku,price,stock
from books where sku='$sku'";
$sql_result = mysql_query($sql,$connection)
or die ("Couldn't get list!");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<link rel="stylesheet" href="simple.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Silk Purse Books - silkpursebooks.com</title>
</head>
<body bgcolor="#ffffff" >
<!--- db --->
<? $location="Details"; include("top.inc"); ?>
<!--- left --->
<tr>
<td>
<?php
echo "<a href=\"main.php\">Back to Main</a>";
?>
Well, one comment: The only way to get here is from the Browse page, and the only place we can really go back to is the Main page; so, no breadcrumb navigation needed here.
Okay, TWO comments: in the couple of years since I wrote this, it seems that "CONDITION" has become a reserved word in MySQL 5, so it has to be `quoted` if you want to use it as the name for a database column. Note the UNIX "backtick" characters used for this purpose -- regular "double" or 'single' quotes do not have the right magic.
As a former COBOL programmer, I can say with authority that reserved words are evil. Shame on you for grabbing yet another batch, MySQL!
<!--- main --->
<td>
<center>
<a href=bookbag.php>Your Bookbag</a> -
<a href=checkout.php>Check Out</a> -
<a href=aboutus.php>About Us</a>
</td>
</tr>
</table>
<table width="90%" align=center border=0 cellpadding=3>
<?
$i = 0;
while ($row = mysql_fetch_array($sql_result)) {
$sku = $row["sku"];
$title = $row["title"];
$author = $row["author"];
$pub = $row["publisher"];
$format = $row["format"];
$isbn = $row["isbn"];
$cond = $row["condition"];
$price = sprintf("%0.2f",$row["price"]);
$stock = $row["stock"];
$BGC="#ffffff";
We play with the BackGround Color (which I'm calling $BGC) a little bit just to make sure the fields don't all run together.
echo " <tr bgcolor=\"#e7f0ff\"> <td colspan=3><b>TITLE</b> <td colspan=2><b>AUTHOR</b> <tr bgcolor=$BGC>
Now that we've laid out the TITLE and AUTHOR headers, we'll display the values from the database...
<td colspan=3>$title <td colspan=2>$author</tr> <tr bgcolor=\"#f0f0ff\">
Repeat for the next row of headers and live data:
<td><b>FORMAT</b><td><b>PUBLISHER</b><td><b>CONDITION</b> <td><b>PRICE</b><td>How Many</tr>
Presenting the fields we got from the database is easy enough:
<tr bgcolor=$BGC> <td>$format<td>$pub<td>$cond<td>$price<td>
Now let's have some real excitement. We want someone to be able to order as many copies of this title as we have, but no more than we have in stock. How do we do that? We generate a dropdown as a numeric input field. There is something we have to iron out first, though -- an HTML "SELECT" does not return a value that we can pass as a PHP parameter! In order to interact with our customer, we have to provide an HTML form. When the customer has chosen the number to purchase, we can transfer that value to the next page using the HTML GET or POST method. (We could also set a PHP SESSION variable, but let's stick with HTML form techiniques for the moment.)
";
echo "<form action=\"addtobag.php\" method=GET><select name=qty>";
for ($q=1;$q<=$stock;$q++) {
echo "<option value=$q>$q";
}
echo "</select>
<input type=hidden name=sku value=\"$sku\">
<input type=submit value=\"Add to Bookbag\"></form>
</td></tr>";
?>
Well, that was fun! Remember, when dealing with a small number, it's safer if you don't ask the customer to type it in. The secret that makes our HTML SELECT command work is that we are using HTML form variables to get qty and sku into our next page -- not PHP variables.
Now we just finish off this page neatly with a contrasting row of color.
<tr bgcolor="#f0f0ff">
<td> <td> <td> <td>
</td></tr></table>
<!--- bottom --->
<br>
<? include("bottom.inc"); ?>
</body></html>
And that's that. When we go to addtobag.php, the variable $qty tells it how many copies of this book to take out of stock.
Of course, you could add things to your flypage. One obvious idea is to store text files (a book review?) and images (cover scans?) as standalone files. Use the SKU as a key and have PHP check to see if a file exists. For instance, if the text file "$sku.txt" exists, include it; if the image file "images/$sku.jpg" exists, generate an "img src" tag for it.
Another way to get the same effect would be to put links into the database, rather than having PHP go looking for files that may or may not exist. That would give you more positive control. Maybe you'll want to set a flag in the database that tells the flypage PHP code to switch to an alternate layout when there are multiple images. The possiblities are endless. As always, I'm going for the simple solution here.
The addtobag.php page is going to be even more exciting.
|
|
|
You are invited to post comments or questions on the SimpleshoPHP forum at SourceForge.net.