Shop - Main Page
This page is usually what the visitor see for the first time when she visit our shop. It consist of five parts. On the left is the category browser. The shopper can click here way through categories to find the product. The right side is where we put the mini shopping cart. If the visitor add a product, this mini cart will show the item. At the very top and bottom are the common header an footer. The top area is usually where we put our store logo. For this tutorial the bottom area is used for displaying the store information (address, email, phone )
The center part is the main area. Here we show the product categories and products. The visitor will ( hopefully ) find her way through the shop, find the item she want, put it into the shopping cart and then buy. What we show in the main area depends on the visitor action. When she first arrive to main page she get the category list. If she click on one of the category then we show the product list for that category. And if she click on a product from the list we show the product detail.
|
Take a look at the code below, it's the code for index.php, the main page Source code : index.php <?php
require_once 'library/config.php'; require_once 'library/category-functions.php'; require_once 'library/product-functions.php'; require_once 'library/cart-functions.php'; $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI']; $catId = (isset($_GET['c']) && $_GET['c'] != '1') ?
$_GET['c'] : 0; // ... more code here First we load the required libraries. The config.php file contain all kind of initialization like setting the database connection, defining constants, and it also load the common libraries. I won't explain the detail, just take a look at the file and i'm sure you will understand what it does. Next is category-functions.php.
This file contain the several functions, they are :
After finish loading the libraries we set a session variable $_SESSION['shop_return_url']. The value for this session is the url of the page that we currently see. This variable is used by the shopping cart. When you click on the 'Continue Shopping' button it will redirect to the url pointed by this session variable. Next step is checking the existence of $_GET['c'] ( category id ) and $_GET['p'] ( product id ). If none exist in the query string we show the category list ( categoryList.php ). If only the category id exist then we show all product in that category ( productList.php ). And if the product id also exist we show that product information ( productDetail.php ). Source code : index.php <?php
// ... previous code include 'header.php'; As you can see from the above code, the main page doesn't do much. It "out sourced" almost everything. The header, navigation, content, mini cart and the footer. Of course this is a good thing because if we need to modify some part ( like the left navigation ) we can do it wit less risk of messing with the other part. Now, lets take a deeper look at the header file. One important usage of this file is to set the page title. You may have noticed it that when you're browsing the categories and products the page title change accordingly. Setting up a descriptive page title is especially important when you consider SEO ( search engine optimization ). You see, search engine put heavy emphasize on the page title so we better get this one thing right. Here how it works. By default we set the page title as "My Online Shop" but if a product id is detected in the query string we search the database to find the name of the product. Then we use the product's name as the page title. If we can't find any product id in the query string we do another test to see if there's a category id in there. If we found one we fetch the category name from the database and use it as the page title. Here is the code : Source : include/header.php <?php // set the default page title if (isset($_GET['p']) && (int)$_GET['p'] > 0) { $result = dbQuery($sql); ?> Next we explore further about the category browsing ( that navigation links on the left section of the page ). |
|
|
|
|
Admin - User management | PHP MySQL Shopping Cart Tutorial : Online Shop - Main Page | Online Shop - Browse Category |
|
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