Adding / editing / deleting product categories
At-first, let us create a form for managing category details. This form should allow managing category name, description, thumbnail and position in the category hierarchy. This is how the form looks in Shop-Script FREE and its HTML code:

|
<form enctype="multipart/form-data" action="category.php" method="POST">
<table width="100%" border="0">
<tr>
<td align="right">
Parent:</td>
<td>
<select name="parent">
<option value="0">Root</option>
<option value="4">Sample category 1</option>
<option value="20"> Child category 1</option>
<option value="76"> Child category 2</option>
< all categories are shown in this select box >
</select>
</td>
</tr>
<tr>
<td align="right">Category name:</td>
<td><input type="text" name="name" value="" size=13></td>
</tr>
<tr>
<td align="right">Logo:</td>
<td><input type="file" name="picture"></td>
</tr>
<tr>
<td> </td>
<td>
(picture not uploaded)</td>
</tr>
<tr>
<td align="right">Description<br>(HTML)</td>
<td><textarea name="desc" rows="7" cols="22"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" value="Save">
<input type="hidden" name="save" value="yes">
<input type="button" value="Cancel" onClick="window.close();">
</p>
</form> |
|
Adding category
Administrator is prompted to fill in a form presented above. Clicking "Save" button will execute following PHP code to save category into the database (category.php):
<?php
if (isset($_POST["save"]) && $_POST["name"])
//save changes
{
db_query("INSERT INTO CATEGORY
(name, parent, products_count, description, picture, products_count_admin) VALUES
('".$_POST["name"]."',".((int)$_POST["parent"]).",0,'".$_POST["desc"]."','',0);"
) or die (db_error());
}
?>
|
Editing category details
Here administrator is prompted to fill in the same form as when adding a category. The only difference is that in this case we add hidden "categoryID" field to the form to indicate that clicking "Save" button should update existing category details, but not add a new one (category.php):
<?php
if (isset($_POST["save"]) && strlen($_POST["name"])>0)
//save changes. Note that 'category name' is mandatory field - if it is not filled in, category will not be saved in the database
{
//if category is moved to any of its subcategories -
//it is necessary to lift all of its subcategories one level up
if (category_Moves_To_Its_SubDirectories($_POST["must_delete"], $_POST["parent"]))
{
//lift up is 'child' required
//get parent ID
$q = db_query("SELECT parent FROM CATEGORY WHERE categoryID<>0 and categoryID='".$_POST["must_delete"]."'") or die (db_error());
$r = db_fetch_row($q);
//lift up
db_query("UPDATE CATEGORY SET parent='$r[0]' WHERE parent='".$_POST["must_delete"]."'") or die (db_error());
//move edited category
db_query("UPDATE CATEGORY SET name='".str_replace("<","<",$_POST["name"])."', description='".$_POST["desc"]."', parent='".$_POST["parent"]."' WHERE categoryID='".$_POST["must_delete"]."'") or die (db_error());
}
else //just move the category
{
db_query("UPDATE CATEGORY SET name='".str_replace("<","<",$_POST["name"])."', description='".$_POST["desc"]."', parent='".$_POST["parent"]."' WHERE categoryID='".$_POST["must_delete"]."'") or die (db_error());
}
if (isset($_FILES["picture"]) && $_FILES["picture"]["name"]) //upload category thumbnail
{
//old thumbnail
$q = db_query("SELECT picture FROM CATEGORY WHERE categoryID='$pid' and categoryID<>0") or die (db_error());
$row = db_fetch_row($q);
//upload new thumbnail
$picture_name = str_replace(" ","_", $_FILES["picture"]["name"]);
if (!move_uploaded_file($_FILES["picture"]["tmp_name"], "./products_pictures/$picture_name")) //failed to upload
{
echo "<center><font color=red>".ERROR_FAILED_TO_UPLOAD_FILE."</font>\n<br><br>\n";
}
else //update information in the database
{
db_query("UPDATE CATEGORY SET picture='$picture_name' WHERE categoryID='$pid'") or die (db_error());
}
//remove old picture...
if ($row[0] && strcmp($row[0], $picture_name) && file_exists("./products_pictures/$row[0]"))
unlink("./products_pictures/$row[0]");
}
}
?>
|
Deleting category
When clicking "Delete" button for a certain category, following actions are performed:
- All products from this category are moved to Root folder.
To do this we only have to execute following SQL-query:
UPDATE `PRODUCT` SET categoryID=`0` WHERE categoryID=`$categoryID_to_delete`;
- Delete category thumbnail using PHP function unlink()
- Delete category record from `SS_categories` table:
DELETE FROM `CATEGORY` WHERE categoryID=`$categoryID_to_delete`;
- Repeat steps 1, 2 and 3 for all child categories of the category that has been already deleted.
Actually, in Shop-Script FREE, we delete all child categories first (step 4) and only then the category itself. This is more correct from the logical point of view.
|