Shop - View Product List
When you click one of the category from the left navigation the main content area will show all product contained in that category. For example if you click on Manga then all four products in this category will be shown. Actually "Manga" doesn't have any product. Those product are belong to it's children categories ( Naruto and HunterXHunter). If you checkout the source code for productList.php you can see that it first call getChildCategories() function so if you browse "Manga" productList.php will show all products in "Manga" ( zero products ) and it's children "Naruto" and "HunterXHunter" ( four products ). Source code : include/productList.php <?php $productsPerRow = 2; $children = array_merge(array($catId), $sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail,
pd_qty, c.cat_id // ... we got more codes here |
| The rest of this page code should be pretty clear to you. There's a SELECT query and since we also want to use paging we feed the query to getPagingQuery(). If you don't know what getPagingQuery() does you should go back and read the page about product list in admin section. Below is a snapshot of the product listing
On productList.php the products are displayed so one row display three products like shown above. The number of product in one row can be changed. You just need to modify the value of $productsPerRow. For example modifying this value into 2 give this look :
And here is the code that make this happen Source code : include/productList.php <?php // ... previous code $numProduct = dbNumRows($result); $i = 0; // format how we display the price // if the product is no longer in stock, tell the customer // .. more code down here
|
| Before printing the product we first check we actually have any product at all. If dbNumRows($result) returns zero that means we have no product in current category. So we show a message saying that there are no products in that category. If we do have some products to show we display them in a table where each column's width depend on how many products to show in a row. Using the above example the column width is 100 / 3 = 33%. Next thing we do is loop through the product list and print each product. If the value of ($i % 3) == 0 we start a new row by printing <tr> and if ($i % 3) == 2 end that row using </tr>. When printing the product info we use displayAmount() function to format the look of the product price. This function is located in library/common.php. Here is what the function look like : Source code : library/common.php function displayAmount($amount)
{ global $shopConfig; return $shopConfig['currency'] . number_format($amount); } What this function does is concat the currency symbol with the formattted amount. So if the amount is 123456 it will be displayed as $123,456. If the product price contains fraction ( like cents ) you need to change this function to this : function displayAmount($amount)
{ global $shopConfig; return $shopConfig['currency'] . number_format($amount, 2); } Notice that we add extra parameter to number_format(). This extra parameter will make the function display the amount with two decimal point such as $19.95 Back to productList.php. After printing the product price we check if we already run out of that product in inventory. If so we print additional message saying that the product is no longer in stock. Since there is always a possibility that the last row we print does not contain three products we check the value of $i % 3 after the loop . If it's greater than zero that means the last row does not have three products in it so we need to print the empty column like shown on the first snapshot. In case the picture isn't clear enough here is the same snapshot but the table border is set to 1. You can see that on the last row we have 2 empty ( merged ) columns
|
|
|
|
|
Online Shop - Browse Category | PHP MySQL Shopping Cart Tutorial : Online Shop - View Product List | Online Shop - View Product Detail |
|
At long last i'm finally able to update this site. Now the shopping cart can handle payment through paypal. As always you have any critiques, questions, comments or problems about this tutorial please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a lot :-) |
PHP MySQL Shopping Cart Tutorial
Copyright © 2005 - 2008 www.phpwebcommerce.com