Shopping Cart Tutorial PHP Shopping Cart Tutorial
Learn how to create an online store with PHP and MySQL
Free shopping cart    Download:
Shop-Script FREE
Shop-Script FREE User Guide (PDF; 0 Kb)
View Live Demo    View online store demo
 Introduction
 Basic concepts
 Technologies
 File structure
 Database structure
 Back end
   Password protection
   Products catalog: structure
   Product catalog: viewing
   Managing categories
   Managing products
   Special offers
   Managing orders
 Front end
   Viewing products catalog
   Product search
   Shopping cart
   Checkout
   PayPal integration
 Security
 Live Demo
 Author
 Terms Of Use

Shopping cart software

 

Viewing products catalog

Products catalog management is one of the core tools of store back end.
In Shop-Script FREE products and categories management is performed in a combined department. Please take a look at the screenshot below: 

How to view category tree?

As described prior, category nesting is unlimited. To implement this we use recurrent function fillTheCList (recurrent means that this function calls itself within its body), which saves categories list into an array, and which is in its turn displayed in Smarty templates. Here is PHP source fragment (includes/admin/sub/catalog_products_categories.php file from Shop-Script FREE archive). I added few comments for you to better understand this technique:

<?php

    
function fillTheCList($parent$level//completely expand category tree and save it into array. Here $parent is currently processed category,and $level indicates category's sublevel:0 for root (core) categories, 1 for their subcategories, etc.
    
{
        
//select all subcategories of currently processed category
        
$q db_query("SELECT categoryID, name, products_count, products_count_admin, parent FROM CATEGORY WHERE categoryID<>0 and parent=$parent ORDER BY name") or die (db_error());
        
$a = array(); //save fetched results into this array
        //now process fetched results (subcategories)
        
while ($row db_fetch_row($q))
        {
            
//add 'level' information to subcategory details
            
$row[5] = $level;
            
//add subcategory to $a array
            
$a[] = $row;
            
//now recurrently call this function again to process all child categories
            //of current category and save results to $b array
            
$b fillTheCList($row[0], $level+1);
            
//add $b to the end of $a
            
for ($j=0$j count($b); $j++)
            {
                
$a[] = $b[$j];
            }
        }
        return 
$a;

    } 
//fillTheCList

?>

Now calling

	$categories = fillTheCList(0,0);

will save expanded categories tree into $categories array.
This array can be easily displayed in Smarty templates then (templates/tmpl1/admin/catalog_products_categories.tpl.html):

<table>

{section name=i loop=$categories}

<tr>

<td>
{section name=j loop=$categories[i].level max=$categories[i].level}    {/section}
<a href="admin.php?dpt=catalog&sub=products_categories&categoryID={$categories[i].categoryID}">{$categories[i].name}</a>
</td>
<td>({$categories[i].products_count})</td>

<td align="right">
<font color="red">[</font>

<a class="small" href="javascript:open_window('category.php?c_id={$categories[i].categoryID}',400,400);">edit</a>

<font color="red">]</font>
</td>

</tr>

{/section}

</table>

Viewing product list

Viewing products is easier because they are stored in PRODUCT table without hierarchical structure. We only fetch products from the current category:

SELECT productID, name, customers_rating, Price, in_stock, picture, big_picture, thumbnail, items_sold, enabled, product_code FROM PRODUCT WHERE categoryID=`$current_categoryID`  ORDER BY name;

save results to $products array and present them on a webpage using Smarty:

{if $products_count eq 0}

<p align="center">{$smarty.const.STRING_EMPTY_CATEGORY}</p>

{else}

<form action="admin.php" method="POST">

<table border="1" width="90%">

<tr>

<td>{$smarty.const.ADMIN_ENABLED}</td>
<td>{$smarty.const.ADMIN_PRODUCT_CODE}</td>
<td>{$smarty.const.ADMIN_PRODUCT_NAME}</td>
<td>{$smarty.const.ADMIN_PRODUCT_RATING}</td>
<td>{$smarty.const.ADMIN_PRODUCT_PRICE}, {$currency_iso_3}</td>
<td>{$smarty.const.ADMIN_PRODUCT_INSTOCK}</td>
<td>{$smarty.const.ADMIN_PRODUCT_PICTURE}</td>
<td>{$smarty.const.ADMIN_PRODUCT_BIGPICTURE}</td>
<td>{$smarty.const.ADMIN_PRODUCT_THUMBNAIL}</td>
<td>{$smarty.const.ADMIN_PRODUCT_SOLD}</td>
<td> </td>
<td> </td>

</tr>

{section name=i loop=$products}

<tr>
<td align="center">
<input type="checkbox" name="enable_{$products[i][0]}" {if $products[i][9] eq 1}value="on" checked{else}value="off"{/if}>
</td>

<td>
<a href="javascript:open_window('products.php?productID={$products[i][0]}',550,600);">{$products[i][10]}</a>
</td>

<td>
<a href="javascript:open_window('products.php?productID={$products[i][0]}',550,600);">{$products[i][1]}</a>
</td>

<td align="right">{$products[i][2]}</td>

<td align="center">
<input type=text name="price_{$products[i][0]}" size="5" value={$products[i][3]}>
</td>

<td align="center">
<input type="checkbox" name="instock_{$products[i][0]}" size="5"{if $products[i][4]>0} checked{/if}>
</td>

<td align="center">
{if $products[i][5] ne ""}{$smarty.const.ANSWER_YES}{else}{$smarty.const.ANSWER_NO}{/if}
</td>
<td align="center">
{if $products[i][6] ne ""}{$smarty.const.ANSWER_YES}{else}{$smarty.const.ANSWER_NO}{/if}
</td>
<td align="center">
{if $products[i][7] ne ""}{$smarty.const.ANSWER_YES}{else}{$smarty.const.ANSWER_NO}{/if}
</td>

<td align="right">{$products[i][8]}</td>

<td align="center">
{if $products[i][5] ne ""}
<a href="admin.php?dpt=catalog&sub=special&new_offer={$products[i][0]}"><img src="images/admin_special_offer.gif" border="0" alt="{$smarty.const.ADMIN_ADD_SPECIAL_OFFERS}"></a>
{else} 
{/if}
</td>

<td>
<a href="javascript:confirmDelete({$products[i][0]}, '{$smarty.const.QUESTION_DELETE_CONFIRMATION}', 'admin.php?dpt=catalog&sub=products_categories&categoryID={$categoryID}&terminate=');"><img src="images/remove.jpg" border="0" alt="{$smarty.const.DELETE_BUTTON}"></a>
</td>

</tr>

{/section}

</table>

<input type="hidden" name="categoryID" value="{$categoryID}">
<input type="submit" value="{$smarty.const.SAVE_BUTTON}">

</form>

{/if}

Managing products & categories

To edit a certain category or product administrator clicks appropriate "Category edit" or "Product name" link.
Read about managing categories  and managing products.

Bringing products and categories in customer storefront  is much like it is done in back end.

 

 


Copyright © 2006 WebAsyst LLC