SimpleshoPHP

Detailed Analysis - Part 2

browse.php

We start with a sanity check (do we have an existing session?), session setup, and database connection:

<?
require_once("startme.inc");

if (! isset($_SESSION["xvalid"])) {
        header("Location: main.php");
        exit;
}

If the $_SESSION variable "xvalid" was not set, that means we came here without going through the main.php page, and the odds that we have a valid parameter (a dept, a title, or an author) are pretty slim. PHP's "header" command allows us to redirect the user back to another page (main.php in this case), as long as we have not written anything to the screen yet. This is important: You cannot use "header" to redirect the user's browser if you have already sent an error message or HTML code.

Now the fun starts. We get to choose from three possible parameters passed in from main.php: $dept, author ($aname, for author name), or title ($bname, for book name). Watch what happens:


if ($dept) {
$sql = "select sku, title, author, format, price from books
 where dept='$dept' and stock > 0 order by title";
}
else if ($aname) {
$sql = "select sku, title, author, format, price from books 
 where author like '%$aname%' and stock > 0 order by title";
}
else if ($bname) {
$sql = "select sku, title, author, format, price from books 
 where title like '%$bname%' and stock > 0 order by author";
}
$sql_result = mysql_query($sql,$connection) 
	or die ("Couldn't get list!");
?>

Assuming one parameter is defined, that's the one we use. The value returned in "$sql_result" depends on which of the three is defined -- but we really don't care which one is used, because the result is just going to be the same thing -- a list of books -- in any case.

Our results are waiting to be displayed. Let's set up our usual HTML stuff:

<!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">

Don't forget to tell the header template where we are, in a user-friendly sort of way:

<? $location="Browse"; include("top.inc");?>

<!--- left --->
<tr>
<td>
<?php if ($dept) {
	echo "<a href=\"main.php\">Main: Departments:</a> <em>$dept</em>";
}
else {
	echo "<a href=\"main.php\">Back to Main</a>";
}
?>

The index page is referred to consistently as "Main." In fact, you notice that it is linked to here as "main.php" -- that is a symbolic link or "alias" that points to index.html.

We can figure out how we came here -- it was either by way of the Departments list or because of a Title/Author search. In the latter case, we just make a backward link that says "Back to Main". If $dept is defined, that means we came here by way of the Department list. We want to help the user to stay oriented, so we present the current location in terms of a hierarchy: "You started at the Main page, came through the list of Departments, and you are now viewing a specific Department."

Time to present the SQL query results! That's going to take the form of a table within a table:

<!--- main --->
<td>
<center>
<a href=bookbag.php>Your Bookbag</a>&nbsp;&nbsp;-&nbsp;&nbsp;  
<a href=checkout.php>Check Out</a>&nbsp;&nbsp;-&nbsp;&nbsp;
<a href=aboutus.php>About Us</a>
</td>
</tr>
</table>
<table width="90%" align=center border=0 cellpadding=3>
<tr bgcolor="#e7f0ff">
<td><b>TITLE - <em>Click for details</em></b><td><b>AUTHOR</b><td>
<b>FORMAT</b><td><b>PRICE</b><td>Add</tr>
<?
$i = 1;
while ($row = mysql_fetch_array($sql_result)) {
	$sku = $row["sku"];
	$title = $row["title"];
	$author = $row["author"];
	$format = $row["format"];
	$price = sprintf("%0.2f",$row["price"]);
	if ($i++ % 2)
            $BGC="#c0e0ff";
	else
            $BGC="#ffffff";
	echo "
	<tr bgcolor=$BGC>
	<td><a href=\"flypage.php?sku=$sku\">$title</a><td>$author<td>
$format<td>$price<td><a href=\"addtobag.php?sku=$sku\">+</a></tr>
	";
}
?>

Once again we use a "while" loop to get rows out of the array, which is how MySQL made the query results available to PHP. Each row we get back from the database is one new table row or "<tr>"

We copy the column values into PHP variables, and let PHP echo those onto the screen using HTML syntax. Each column gets its own table detail or "<td>."

Two things might need some explaining. First, the sprintf function helps us to display dollars and cents. Without that, a even-dollar price would display as a single digit ("3") and worse, $3.50 would display as "3.5". Neither of those looks like a US dollars-and-cents price.

Second, the $BGC (for BackGround Color) variable uses a bit of math magic to keep track of whether we are on an odd or an even line. We alternate the background color to keep each row visibly separated from the ones above and below. Even-numbered rows are white, odd rows are light gray. We could have left that out, but once you've seen the site both ways, you realize that it's the sort of detail you miss when you don't have it.

You'll notice that we generate a link using the book's title, but the value that it passes to "flypage.php" is the SKU, not the title. Similarly, if the browser wants to add the book to her bookbag, we provide a link to take her directly to "addtobag.php," again using the SKU. We'll get to both of those pages soon.

That's it for the browse page. It's time to wrap things up with our usual footer material. Note the "colspan" value is 5 because of all the items we display per book. We're also still in PHP mode, so we have to "echo" our HTML content to the browser. That's also why we use backslashes with the double-quotes in our definition of bgcolor -- if we didn't "escape" those quotes, they would end the output prematurely.

echo "
<tr>
<td colspan=5 bgcolor=\"#e7f0ff\">
<p> </p></td></tr>
<tr align=center>
<td colspan=5>";
include("bottom.inc"); ?>
</td></tr></table>
</body></html>


Our next stop is the flypage.php page, which presents a more detailed view of a single item. You might try to write one yourself before I present mine -- you have seen all the tools needed to create it.


The Technical Walkthrough

Summary

Detailed descriptions

SimpleshoPHP Home


You are invited to post comments or questions on the SimpleshoPHP forum at SourceForge.net.

SourceForge.net Logo


Copyright 2003-2005, Kevin Martin, dba Brass Cannon Consulting.
The project "SimpleshoPHP" is Free Software, distributed
under the LGPL as described at opensource.org