Location: PHPKode > projects > Simple PHP Shopping cart - asaancart > asaancart v-0.9/admin/add_cat.php
<?php
session_start();
/*
asaancart - easy shopping cart solution
---------------------------------------

Copyright 2009 Nasir Ahmad Khan
Email: hide@address.com

This file is part of asaancart - open source easy shopping cart solution.

asaancart is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

asaancart is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with asaancart.  If not, see <http://www.gnu.org/licenses/>.

*/

include("../config/config.php");
include("includes/chk_login_status_inc.php");

$category_name = $_POST['category_name'];
$parent_cat_id = $_POST['parent_cat_id'];
$category_id = $_POST['category_id'];
$category_description = $_POST['category_description'];
$category_thumb_image = $_POST['category_thumb_image'];
$category_full_image = $_POST['category_full_image'];


if($_POST['btn_submit']=="Create")
{	
	if($category_name != ""){
		if($parent_cat_id=="root"){
		
		
		//upload image for cat
		$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/'.APP_ROOT_DIR.'/category_images/';
		
		if(basename($_FILES['category_thumb_image']['name'])!=''){
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				$image_filename_thumb = "$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				if (move_uploaded_file($_FILES['category_thumb_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			} //end if 
			
		if(basename($_FILES['category_full_image']['name'])!=''){
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_full_image']['name']);
				$image_filename_full = "$category_name"."_".basename($_FILES['category_full_image']['name']);
				if (move_uploaded_file($_FILES['category_full_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			} //end if 
			
			
		//end upload
		
		
		//enter cat
		//check if cat name already entered
		$sql_chk = "SELECT category_id FROM category WHERE category_name='$category_name'";
		$results_chk = mysql_query($sql_chk);
		$found = mysql_num_rows($results_chk);
		if($found>0){
		
		}else{
			$sql = "INSERT INTO category (category_name, category_description, category_thumb_image, category_full_image) VALUES ('".$category_name."','".$category_description."','".$image_filename_thumb."','".$image_filename_full."')";
			$results = mysql_query($sql);
		}
		
		
		
		}else{
		
		
		//upload image for cat
		$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/'.APP_ROOT_DIR.'/category_images/';
		
		if(basename($_FILES['category_thumb_image']['name'])!=''){
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				$image_filename_thumb = "$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				if (move_uploaded_file($_FILES['category_thumb_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			} //end if 
			
		if(basename($_FILES['category_full_image']['name'])!=''){
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_full_image']['name']);
				$image_filename_full = "$category_name"."_".basename($_FILES['category_full_image']['name']);
				if (move_uploaded_file($_FILES['category_full_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			} //end if 
			
			
		//end upload
		
		
		//enter cat
		$sql = "INSERT INTO category (category_name, category_description, category_thumb_image, category_full_image) VALUES ('".$category_name."','".$category_description."','".$image_filename_thumb."','".$image_filename_full."')";
		$results = mysql_query($sql);
		//get cat id
			$sql = "SELECT category_id FROM category WHERE category_name='$category_name'";
		$results = mysql_query($sql);
		
		while($row = mysql_fetch_array($results) )
		{
			$category_id = $row[0];		
		}
		//insert as sub cat
		$sql = "INSERT INTO sub_category (category_id, parent_cat_id) VALUES ($category_id, $parent_cat_id)";
		$results = mysql_query($sql);
		}
		
		
		
		
		$smarty->assign('msg_cat','Done: Created Successfully');
	}else{
		$smarty->assign('msg_cat','Error: Please enter category name $category_name');
	}

}

if($_POST['btn_submit']=="Save")
{	
	if($category_name != ""){

		//upload image for cat
		$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/'.APP_ROOT_DIR.'/category_images/';
		
		//images details
		$sql = "SELECT * FROM category WHERE category_id=".$category_id;
		$results = mysql_query($sql);
		
		while($row = mysql_fetch_assoc($results) )
		{
			$image_full_temp = $row['category_full_image'];	
			$image_thumb_temp = $row['category_thumb_image'];		
		}
		//end img details
		
		
		if(basename($_FILES['category_thumb_image']['name'])!=''){
				//unlink($uploaddir.$image_thumb_temp);
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				$image_filename_thumb = "$category_name"."_".basename($_FILES['category_thumb_image']['name']);
				if (move_uploaded_file($_FILES['category_thumb_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			}else{
				$image_filename_thumb = $image_thumb_temp;
			} //end if 
			
		if(basename($_FILES['category_full_image']['name'])!=''){
				//unlink($uploaddir.$image_full_temp);
				$uploadfile = $uploaddir ."$category_name"."_".basename($_FILES['category_full_image']['name']);
				$image_filename_full = "$category_name"."_".basename($_FILES['category_full_image']['name']);
				if (move_uploaded_file($_FILES['category_full_image']['tmp_name'], $uploadfile)) {
				//echo "File is valid, and was successfully uploaded.\n";
				} else {
					$smarty->assign('msg_cat','Possible file upload attack!');
				}
				$uploadfile = "";
			}else{
				$image_filename_full = $image_full_temp;
			} //end if 
			
			
		//end upload
		
		
		//update cat
		$sql = "UPDATE category SET category_name='$category_name', category_description='$category_description', category_thumb_image='$image_filename_thumb', category_full_image='$image_filename_full' WHERE category_id=".$category_id;
		$results = mysql_query($sql);
		
		
		
		
		$smarty->assign('msg_cat','Done: Created Successfully');
	}else{
		$smarty->assign('msg_cat','Error: Please enter category name $category_name');
	}

} 
//manage
$sql = "SELECT * FROM category ORDER BY category_name";
$results = mysql_query($sql);
$total_cat = mysql_num_rows($results);
$smarty->assign('total_cat', $total_cat);

while($row = mysql_fetch_assoc($results) )
{
	$cat_list[] = $row;		
}

$smarty->assign('cat_list', $cat_list);
//end manage

$smarty->display('manage_cat.tpl');
?>
Return current item: Simple PHP Shopping cart - asaancart