Admin - View Product

 

This page list all the products we have. We can see all products or just products from certain category. From this page we can see the product detail , add new product, modify and delete.

Below is what the page look like. The table shows the product name, thumbnail, category, and modify and delete link. We show the product image if it exist. In case we haven't supply the image the default image is shown instead.

Online Shop Admin - View Product

There's a javascript function called viewProduct() attached to the combo box on the top right portion of the page. If you select a category then the page will show product list only from that category. For example if you choose "Naruto" the this page will look like this :

Online Shop Admin - View Product In Category

If you wish to view all products again just choose "All Category" from the combo box

The code for this page is in admin/product/list.php. It's a very simple page really. It just perform a simple SELECT query and loop through the result to print the contents. If the products returned from the query is more than five we print the paging links to navigate from one result page to another. The code snippet below shows how the paging is done.

Source code : admin/product/list.php

<?php
if (!defined('WEB_ROOT')) {
   exit;
}


if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
   $catId = (int)$_GET['catId'];
   $sql2 = " AND p.cat_id = $catId";
   $queryString = "catId=$catId";
} else {
   $catId = 0;
   $sql2 = '';
   $queryString = '';
}

// for paging
// how many rows to show per page
$rowsPerPage = 5;

$sql = "SELECT pd_id, c.cat_id, cat_name, pd_name, pd_thumbnail
            FROM tbl_product p, tbl_category c
            WHERE p.cat_id = c.cat_id $sql2
            ORDER BY pd_name";
$result = dbQuery(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage, $queryString);

$categoryList = buildCategoryOptions($catId);

?>

Instead of executing the query directly like this dbQuery($query) we add some paging code first to the sql query by feeding it to getPagingQuery() along with how many results that we want to show on each page. The getPagingQuery() function is located in library/common.php. Here is the code :

Source code : library/common.php

function getPagingQuery($sql, $itemPerPage = 10)
{
   if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
      $page = (int)$_GET['page'];
   } else {
      $page = 1;
   }

   // start fetching from this row number
   $offset = ($page - 1) * $itemPerPage;

   return $sql . " LIMIT $offset, $itemPerPage";
}

This function first check the page number. When you click on a paging link there's a page variable embedded in the query string. If the function can't find any page variable on the query string then it just assume that the first page is wanted.

Paging on MySQL is done using the LIMIT keyword. The offset is the index where we want to start fetching the result. We also supply how many result that we want .

Now after we get the paging query and execute it the next thing we must do is making the page links. This is done by getPagingLink() function. This function is the one responsible for printing the paging link you see on the bottom of the product list. The function only does the followings :

  1. Find out how many total results returned by a query
  2. Calculate how many pages the results should be split into
  3. Determine the first and last page
  4. Print page link from first to last.

Somehow i don't feel like explaining the paging process in detail here. If you're interested you can read the full tutorian on pagination here : paging tutorial on www.php-mysql-tutorial.com

Next we discuss about adding a new product. When you click on the "Add Product" button you will be taken to the add product screen.

 

Get custom programming done at ScriptLance.com!

 

 

Admin - Add Product PHP MySQL Shopping Cart Tutorial : Admin - View Product Admin - Edit Product
Google
 
Web www.phpwebcommerce.com
 

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 :-)

 

"I made an extra $378 last week promoting just 1 site. Click here to see which site is it. Join today!"

I made extra $378 last week. Click here to learn how



PHP MySQL Shopping Cart Tutorial
Copyright © 2005 - 2009 www.phpwebcommerce.com