We're going to be conservative to a fault in our HTML -- we're going to make extensive, even excessive use of tables. Why? Because they're simple. (That's the short answer to any "why" question that arises. We may as well get that out of the way right now.)
So... here's the basic SimpleshoPHP page:
<table border=1 width=90%> <tr><td colspan=3>banner area</tr> <tr><td>left<td>main display area<td>right</tr> <tr><td colspan=3>footer</tr> </table>
And here's what it looks like when it's rendered by your browser (we set "border=1" for now so you can see each section clearly):
display area | ||
So far so good, right? We are going to have a lot of fun with each of those table detail ("TD") items, but this gives us a clean skeleton to start our development. The banner will have a graphic, the left panel will provide navigation, the main panel will change depending on which page we're at, and the right panel will come and go as needed; we will have to adjust the number of columns -- our "colspan" values -- to handle that.
It turns out to be mind-bogglingly easy to generate a list of links, which is how we will populate the left panel of our main page. Once you have established a connection to the MySQL database, it is literally one line of SQL code to get a list of unique "departments," and a mere snippet of PHP to display those departments as HTML links. Let's use some pseudo-code to describe how it works.
Pseudo-code is not a real programming language, but it's "close enough" to one for a developer to turn it into a working program in the language of his or her choice... yet it's clear enough (we hope!) for an intelligent layperson to understand it. |
create session connect to database SQL: "select distinct dept from books;" into variable $dept while (row) phpecho "<a href=\"browse?dept=$dept\">dept"; done
We create a PHP session, which is a built-in function of PHP; it gives back a session ID, a long string of hexadecimal numbers. This ID will be passed back and forth between the server and your browser for as long as they agree you are still in the same "session." We will need this for your shopping cart, or as we call it, your book bag.
We connect to the database (more built-in PHP functions) and ask MySQL to return each distinct, different, department it finds in the books table -- there are 400 or so books in the database, thus 400 rows in the books table, but there are only a dozen or so distinct departments.
We tell PHP to run a while loop, meaning "do this while there are still new rows" -- and thus we get those dozen departments.
We then take advantage of PHP's formatting powers to turn those twelve department codes into both the visible link we display, and a parameter we pass to the next page, the "browse" page.
Let's try it with some sample values. Here's a database entry for a book:
TITLE AUTHOR PUBLISHER FORMAT PRICE DEPT SKU My Life as a Frog Bill French Madhouse PB 7.95 Fiction MLFBF01
"Fiction" will obviously come up as one of the values for "dept". Note that we are not using a bizarre code that would be looked up in yet another table; we are just using plain words. The HTML that will be generated and displayed because this book exists will look like this:
<a href="browse?dept=Fiction">Fiction</a>
By inserting those pointy <tags>, we cause PHP to generate an HTML link. We can click on that link in a browser, and doing so will open the "browse" page and send it a variable named "dept" with the value "Fiction".
In the browse page, we will use the value given to the variable "dept" to do another query, and return only the books that are in that department:
select [everything] from books where dept="$dept";
The browse page will run that new SQL query and get back all the books that have a dept value equal to "Fiction". Our sample book will be one of those, so let's see what we will display for it:
<a href="flypage?sku=MLFBF01">My Life As a Frog</a> Bill French
PB 7.95 <a href="buy?sku=MLFBF01">Buy it now</a>
You may have seen the term "SKU" used before. It stands for "Stock Keeping Unit." It is simply a unique identifier. For instance, a paperback and a hardcover edition of the same book would have different SKUs.
Did you notice that we created two links this time? If the customer clicks on the book's title, we will display a "flypage" with more detailed information about the book. If the customer decides to skip the flypage and instead clicks on the link to "buy it now", we will pass the SKU to the "buy" page as a parameter.
The general case, the template that we used to generate the line above, looks like this:
<a href="flypage?sku=$sku">$title</a> $author $format $price <a href="buy?sku=$sku">Buy it now</a>
Again, "flypage" and "buy" are PHP-based pages, just as "browse" was; the "?sku=" is how we pass a parameter to them; "$sku" is the PHP variable that contains the SKU, which we got from the database along with the title, author, and other information.
That's really the whole system in a nutshell, though we need to flesh out the details. First we'll complete the overview by walking through the bookbag code and the checkout process. Then we'll come back and look at actual commented code.
|
|
|
You are invited to post comments or questions on the SimpleshoPHP forum at SourceForge.net.