Thiết kế website bán hàng | Trang Admin: quản lý danh mục và sản phẩm

Ở bài này, mình sẽ hướng dẫn các bạn viết trang admin quản lí danh mục và sản phẩm, mình viết cho quản lý đa cấp và cách quản lý giống với các oscommerce khác
1. file category.php: danh sách/xóa danh mục và sản phẩm

<?php include ‘inc_checklogin.php’;
$parentid = intval($_GET[‘parentid’]);

if($_GET[‘delete’]){
mysql_query(“DELETE FROM lesson_category WHERE id = “.intval($_GET[‘delete’]));
mysql_query(“DELETE FROM lesson_product WHERE catid = “.intval($_GET[‘delete’]));
header(‘location: category.php?parentid=’.$parentid);
exit();

}
if($_GET[‘prodel’]){
mysql_query(“DELETE FROM lesson_product WHERE id = “.intval($_GET[‘prodel’]));
mysql_query(“DELETE FROM lesson_comment WHERE proid = “.intval($_GET[‘prodel’]));
header(‘location: category.php?parentid=’.$parentid);
exit();

}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Shop Admin</title>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
</head>

<body>
<?php include ‘inc_header.php’;?>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”18%” valign=”top”><?php include ‘inc_menu.php’;?></td>
<td width=”82%” valign=”top”><h2>Chuyên mục/Sản phẩm</h2>
<h4><a href=”category_edit.php?id=insert&parentid=<?php echo $parentid;?>”>Chuyên mục mới</a><?php echo$parentid?’ | <a href=”product_edit.php?id=insert&parentid=’.$parentid.'”>Sản phẩm mới</a>’:”;?></h4>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr bgcolor=”#CCCCCC”>
<th width=”4%”>Kiểu</th>
<th width=”88%”>Tên</th>
<th width=”8%”>Công cụ</th>
</tr>
<?php 
$sql = “SELECT * FROM lesson_category WHERE parentid = $parentid”;
$sql = mysql_query($sql) or die(mysql_error());
while($rs = mysql_fetch_assoc($sql)){ ?>
<tr>
<td align=”center”>C</td>
<td><a href=”category.php?parentid=<?php echo $rs[‘id’];?>”><?php echo $rs[‘name’];?></a></td>
<td align=”center”><a href=”category_edit.php?id=<?php echo $rs[‘id’];?>&parentid=<?php echo $parentid;?>”>Sửa</a> <a href=”category.php?delete=<?php echo $rs[‘id’];?>&parentid=<?php echo $parentid;?>”>Xóa</a></td>
</tr>
<?php } ?>

<?php
$sql = “SELECT * FROM lesson_product WHERE catid = $parentid”;
$sql = mysql_query($sql);
while($rs = mysql_fetch_assoc($sql)){ ?>
<tr>
<td align=”center”>Sản phẩm</td>
<td><?php echo $rs[‘name’];?></td>
<td align=”center”><a href=”product_edit.php?id=<?php echo $rs[‘id’];?>&amp;parentid=<?php echo $rs[‘catid’];?>”>Sửa</a> <a href=”category.php?prodel=<?php echo $rs[‘id’];?>&amp;parentid=<?php echo $rs[‘catid’];?>”>Xóa</a></td>
</tr>
<?php } ?>
</table>
<h6>Ghi chú:</h6>
C: category<br />

</td>
</tr>
</table>
<?php include ‘inc_footer.php’;?>
</body>

</html>

2. file category_edit.php: cập nhật/thêm mới danh mục

<?php include ‘inc_checklogin.php’;
$parentid = intval($_GET[‘parentid’]);
if($_POST){
$name = addslashes($_POST[‘name’]);

if($_GET[‘id’]==’insert’){
mysql_query(“INSERT INTO lesson_category(name,parentid) VALUES(‘$name’,’$parentid’)”);
}else{
mysql_query(“UPDATE lesson_category SET name = ‘$name’ WHERE id = “.intval($_GET[‘id’]));
}
header(‘location: category.php?parentid=’.$parentid);
exit();
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Shop Admin</title>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
</head>

<body>
<?php include ‘inc_header.php’;?>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”18%”><?php include ‘inc_menu.php’;?></td>
<td width=”82%”><h2><?php echo $_GET[‘id’]==’insert’?’New’:’Update’;?> Category</h2>

<form id=”form1″ name=”form1″ method=”post” action=””>
<?php 
$result = array();
if(is_numeric($_GET[‘id’])){
$sql = “SELECT * FROM lesson_category WHERE id = “.$_GET[‘id’];

$sql = mysql_query($sql);
$result = mysql_fetch_assoc($sql);
}
?>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”19%”>Tên</td>
<td width=”81%”><input type=”text” name=”name” id=”name” value=”<?php echo $result[‘name’];?>” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”button” id=”button” value=”Submit” /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php include ‘inc_footer.php’;?>
</body>

</html>

3. file product_edit.php: cập nhật/thêm mới sản phẩm

<?php include ‘inc_checklogin.php’;
include ‘../includes/functions.php’;
$parentid =intval($_GET[‘parentid’]);
if($_POST){
$name = addslashes($_POST[‘name’]);
$desc = addslashes($_POST[‘desc’]);
$price = addslashes($_POST[‘price’]);
$image = upload(‘image’,’../upload/’);
if($_GET[‘id’]==’insert’){
mysql_query(“INSERT INTO lesson_product(name,catid,`desc`,price,image) VALUES(‘$name’,’$parentid’,’$desc’,’$price’,’$image’)”);
}else{
mysql_query(“UPDATE lesson_product SET name = ‘$name’,`desc` = ‘$desc’,price = ‘$price’ WHERE id = “.intval($_GET[‘id’]));
if($image) mysql_query(“UPDATE lesson_product SET image = ‘$image’ WHERE id = “.intval($_GET[‘id’]));
}
header(‘location: category.php?parentid=’.$parentid);
exit();
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Shop Admin</title>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
</head>

<body>
<?php include ‘inc_header.php’;?>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”18%” valign=”top”><?php include ‘inc_menu.php’;?></td>
<td width=”82%”><h2><?php echo $_GET[‘id’]==’insert’?’New’:’Update’;?> Sản phẩm</h2>

<form action=”” method=”post” enctype=”multipart/form-data” name=”form1″ id=”form1″>
<?php 
$result = array();
if(is_numeric($_GET[‘id’])){
$sql = “SELECT * FROM lesson_product WHERE id = “.$_GET[‘id’];

$sql = mysql_query($sql);
$result = mysql_fetch_assoc($sql);
}
?>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”19%”>Name</td>
<td width=”81%”><input type=”text” name=”name” id=”name” value=”<?php echo $result[‘name’];?>” /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name=”desc” cols=”50″ rows=”5″ id=”desc”><?php echo $result[‘desc’];?></textarea></td>
</tr>
<tr>
<td>Price</td>
<td><input type=”text” name=”price” id=”price” value=”<?php echo floatval($result[‘price’]);?>” /></td>
</tr>
<tr>
<td>Image</td>
<td>
<?php echo $result[‘image’]?'<img src=”../upload/’.$result[‘image’].'” width=”50″ height=”50″ />’:”;?>
<input type=”file” name=”image” id=”image” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”button” id=”button” value=”Submit” /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php include ‘inc_footer.php’;?>
</body>

</html>

Trong phần cập nhật sản phẩm bạn cần 1 hàm upload file, thêm hàm upload vào vào file includes/functions.php

function upload($input_name,$dir){
$userfile=@$_FILES[$input_name][‘name’];
if($userfile==” || strlen($userfile)==0) return false;
$temp = preg_split(‘/[\/\\\\]+/’, $userfile);
$file = $temp[count($temp)-1];
$upload_file = $dir.time().$file;
if(file_exists($upload_file)) $upload_file = $dir.time().’-‘.$file;
if(move_uploaded_file($_FILES[$input_name][“tmp_name”], $upload_file)) return $upload_file;
return false;
Chúc bạn thành công
FOLLOW US

Leave a Reply

Your email address will not be published. Required fields are marked *