Admin - Add Category
Here the admin can add new product category for the online shop. The information we need are the category name, description, image. Both the name and description are mandatory but the image is not. If we don't have the category image we can leave the field blank. Of course it's not recommended because when the customer come to the shop she will see the default image. The category description here will not be shown anywhere on the shop. It's only purpose is to let the shop owner / admin to know what the category is all about. Take a look at the snapshot below, nothing fancy right?
Take a look at the form source code. The form has a hidden variable called hidParentId. The value is set from category/list.php as explained on the previous page. Go look at the source code and scroll to the bottom you will see this code :
<input name="btnAddCategory" type="button" id="btnAddCategory"
value="Add Category" class="box" onClick="addCategory(<?php echo $catId; ?>)"> |
|
When you submit the form the process then handed to processCategory.php. All kind of category processing ( add, modify, delete ) are done in this file. On top of the script there's a simple switch to call the appropriate function based on the action required.
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'addCategory' :
addCategory();
break;
case 'modifyCategory' :
modifyCategory();
break;
case 'deleteCategory' :
deleteCategory();
break;
case 'deleteImage' :
deleteImage();
break;
default :
// if action is not defined or unknown
// move to main category page
header('Location: index.php');
}
On the add category form the form action is set as processCategory.php?action=addCategory so if you look at the code above the script will call addCategory();. If no action is defined we just redirect to category main page. When saving the product image there is a possibility of name conflict. It may seem weird for two categories to have the same image name, but in some cases it can happen. To avoid such conflict we will generate a new name for each category image we upload using the combination of rand(), time() and md5() functions like this :
// get the image extension
$ext = substr(strrchr($image['name'], "."), 1); // generate a random new file name to avoid name conflict $imagePath = md5(rand() * time()) . ".$ext"; The image name wil then become something like 6c444ed816ce251d610c25154dc28462.jpg. Now it's almost impossible for us to ever hit the name conflict problem. We will use the same name generation for the product image and thumbnail. How does it work ? The time() function will return the number of seconds elapsed since the beginning of ( computer ) time which is January 1, 1970. Using rand() function we get a random value less or equal to the number of seconds. We need to use rand() because this shopping cart can have more than one admin. If two admins happen to submit the form at the same second the result of time() will be the same. As the final step md5() use the random value and return the hash ( a string with 32 characters ). If you feel that using 32 characters for a filename is too much you can use substr() function to cut it like this : // get the image extension
$ext = substr(strrchr($image['name'], "."), 1); // generate a random new file name to avoid name conflict $imagePath = substr(md5(rand() * time()), 0, 10) . ".$ext"; The code above will use only the first ten characters as the file name. Next is the modify category page. |
|
|
|
|
Admin - View Category | PHP MySQL Shopping Cart Tutorial : Admin - Add Category | Admin - Edit 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