0% found this document useful (0 votes)
262 views12 pages

PHP E-commerce Pagination Code

The document discusses code for pagination, listing products, updating and deleting products in PHP and MySQL. Some key points: - It shows code for pagination with LIMIT and OFFSET to display products in pages of a set page size. - Code is provided to list and display products from the database with images, prices and buttons to view, edit or delete each product. - Functions are included to retrieve related data like categories and subcategories to populate dropdowns for updating a product. - Form code demonstrates updating a product by retrieving the existing data, validating input, and updating the database record. - Functions are also shown to delete a product by ID and retrieve a product's details for

Uploaded by

Thục Trinh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views12 pages

PHP E-commerce Pagination Code

The document discusses code for pagination, listing products, updating and deleting products in PHP and MySQL. Some key points: - It shows code for pagination with LIMIT and OFFSET to display products in pages of a set page size. - Code is provided to list and display products from the database with images, prices and buttons to view, edit or delete each product. - Functions are included to retrieve related data like categories and subcategories to populate dropdowns for updating a product. - Form code demonstrates updating a product by retrieving the existing data, validating input, and updating the database record. - Functions are also shown to delete a product by ID and retrieve a product's details for

Uploaded by

Thục Trinh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Code Phân Trang

Code sau tab Body


<?php
require_once("dbconn.php");
require_once("phpfunction.php");

$pageSize=5;
$pageNum=1;
$totalRows=0;

if(isset($_GET['pageNum'])==true) $pageNum=$_GET['pageNum'];
$startRow=($pageNum-1)*$pageSize;

$sqlcount="SELECT count(*)
FROM sanpham, loaisp, chungloai
WHERE sanpham.idLoai=loaisp.idLoai AND sanpham.idCL=chungloai.idCL
ORDER BY idSP DESC";

$rs=mysql_query($sqlcount);
$row_rs=mysql_fetch_row($rs);
$totalRows=$row_rs[0];

$sql="SELECT idSP, TenSP, MoTa, UrlHinh, Gia, sanpham.AnHien, TenLoai, TenCL


FROM sanpham, loaisp, chungloai
WHERE sanpham.idLoai=loaisp.idLoai AND sanpham.idCL=chungloai.idCL
ORDER BY idSP DESC";

$sql=sprintf("%s LIMIT %d, %d", $sql, $startRow, $pageSize);

$sanpham=mysql_query($sql) or die (mysql_error());


?>

Code đường link


<a href="#">Thêm</a>

1
Code vòng lặp
<?php while($row_sanpham=mysql_fetch_assoc($sanpham)){?>
<!--Bat dau lap-->
<tr>
<td align="center">
<input type="checkbox" name="chon" id="chon" value="<?=$row_sanpham['idSP']?>"/></td>
<td align="center">
<span>idSP: <?=$row_sanpham['idSP']?></span><br/>
<span>Giá: <?=$row_sanpham['Gia']?></span><br/>
</td>
<td valign="top"><br/>
<p> <?=$row_sanpham['TenSP']?>
<span> (<?=$row_sanpham['TenLoai']?> </span> / <span> <?=$row_sanpham['TenCL']?>)</span>
</p>
<p class="mota">
<img src="<?php echo $row_sanpham['UrlHinh'];?>" align="left" width="70" height="70" border="1"/>
<?=$row_sanpham['MoTa'];?>
</p>
<td align="center" >|<a href="#"> Chỉnh </a>|<a href="sanpham_xoa.php?idSP=<?php echo
$row_sanpham['idSP'];?>" onclick ="return confirm('Ban co muon xoa khong?');"> Xóa </a>|
</td>
</tr>
<?php } ?>

Code thanh phân trang


<?php echo pagesListLimit($totalRows,$pageSize);?>

Code Chi tiết sản phẩm


Code sau tab Body
<?php
$sql=mysql_query("select TenSP, UrlHinh, Gia, SoLuongTonKho, MoTa, TenLoai, TenCL
from sanpham, loaisp, chungloai
where sanpham.idLoai=loaisp.idLoai and sanpham.idCL = chungloai.idCL
and idSP=$idSP") or die (mysql_error());
$chitiet_sp=mysql_fetch_assoc($sql);
?>
Code lấy dữ liệu
<img src="<? echo $chitiet_sp['UrlHinh'];?>" width="180" height="180" border="2" />
<? echo $chitiet_sp['TenSP'];?>
<? echo number_format($chitiet_sp['Gia']);?>
<? echo $chitiet_sp['TenCL']."/".$chitiet_sp['TenLoai'];?>

2
<? if($chitiet_sp['SoLuongTonKho']>0)echo "Còn hàng"; else echo "Hết hàng"; ?>
Code nút mua hàng
<a href="#" idSP="<?php echo $chitiet_sp['idSp']; ?>" class="dathang"><img src="image/muahang.gif"/></a>

Code xóa SP
Code gọi hàm xóa SP và truyền biến đến trang sanpham_xoa.php
<a href="sanpham_xoa.php?idSP=<?php echo $row_sanpham['idSP'];?>" onclick="return confirm('Ban co muon xoa
khong?');"> Xóa </a>

Hàm xóa SP (sanpham_xoa.php)


<?php require_once("dbconn.php"); ?>

<?php
if (isset($_GET['idSP'])==true)
{
$idSP=$_GET['idSP'];
settype($idSP,"int");
if($idSP>0)
{
$sql="DELETE FROM sanpham WHERE idSP=$idSP";
mysql_query($sql) or die (mysql_error());
}
header("location: DSSanPham_PhanTrang.php");
}
?>

Code Cập nhật SP

Code đầu trang cập nhật


<?php require_once("dbconn.php");
$idSP=-1;
if (isset($_GET['idSP'])==true)
{
$idSP=$_GET['idSP']; settype($idSP,"int");
$sql="SELECT * FROM sanpham WHERE idSP=$idSP";
$sp=mysql_query($sql);
$row_sp=mysql_fetch_assoc($sp);
}

if (isset($_POST['btnCapnhat'])==true)
{
//Tiếp nhận dữ liệu từ form

3
$TenSP = $_POST['TenSP'];
$Gia = $_POST['Gia'];
$UrlHinh = $_POST['UrlHinh'];
$AnHien = $_POST['AnHien'];
$SoLuongTonKho = $_POST['Soluong'];
$idCL = $_POST['idCL'];
$idLoai = $_POST['idLoai'];
$MoTa = $_POST['MoTa'];
$Ghichu = $_POST['Ghichu'];
//Kiểm tra dữ liệu đã nhận
settype($AnHien,"int");
settype($Gia,"int");
settype($idCL,"int");
settype($idLoai,"int");
settype($SoLuongTonKho,"int");
$TenSP = trim(strip_tags($TenSP));
$MoTa = trim(strip_tags($MoTa));
$Ghichu = trim(strip_tags($Ghichu));
$UrlHinh = trim(strip_tags($UrlHinh));
if (get_magic_quotes_gpc()== false)
{
$TenSP = mysql_real_escape_string($TenSP);
$MoTa = mysql_real_escape_string($MoTa);
$Ghichu = mysql_real_escape_string($Ghichu);
$UrlHinh = mysql_real_escape_string($UrlHinh);
}
$Ngay=date("Y-m-d");
//chèn vào db
$sql="UPDATE sanpham
SET TenSP='$TenSP', MoTa='$MoTa', UrlHinh='$UrlHinh', Gia=$Gia,
SoLuongTonKho=$SoLuongTonKho, idCL=$idCL, idLoai=$idLoai,
AnHien=$AnHien, GhiChu='$GhiChu', NgayCapNhat='$Ngay'
WHERE idSP=$idSP";
mysql_query($sql) or die(mysql_error());
header("location:index.php");
}
?>
Code sau tab title
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function()
{
$("#idCL").change(function()
{
var idCL=$(this).val();
4
$("#idLoai").load("layloaisp.php?idCL="+ idCL);
});
});
</script>
Code sau tab Body
<?php $sql="SELECT idCL, TenCL FROM chungloai ORDER BY TenCL ASC";
$CL=mysql_query($sql) or die(mysql_error()); ?>
<?php ob_start();?>
Code combo chủng loại
<select name="idCL" id="idCL">
<?php while ($row_CL = mysql_fetch_assoc($CL)) { ?>
<option value="<?php echo $row_CL['idCL'];?>"> <?php echo $row_CL['TenCL'];?> </option> <?php } ?>
</select>
Code combo Loại
<select name="idLoai" id="idLoai">
<script>
$(document).ready(function() {
$("#idCL").val({idCL});
$("#idLoai").load("layloaisp.php?idCL={idCL}",null, GanLoaiSP);
});
function GanLoaiSP()
{$("#idLoai").val({idLoai}); }
</script>
<option value="0"> Chọn loại </option>
</select>
Code sau tab đóng của form
<?php
$str=ob_get_clean();
$str = str_replace("{TenSP}" , $row_sp['TenSP'],$str);
$str = str_replace("{MoTa}" , $row_sp['MoTa'],$str);
$str = str_replace("{UrlHinh}" , $row_sp['UrlHinh'],$str);
$str = str_replace("{Gia}" , $row_sp['Gia'],$str);
$str = str_replace("{SoLuongTonKho}",$row_sp['SoLuongTonKho'],$str);
$str = str_replace("{idCL}" , $row_sp['idCL'],$str);
$str = str_replace("{idLoai}" , $row_sp['idLoai'],$str);
$str = str_replace("{An}" , ($row_sp['AnHien']==0)? "checked=checked":"",$str);
$str = str_replace("{Hien}" , ($row_sp['AnHien']==1)? "checked=checked":"",$str);
echo $str;
?>
Code của textbox
<input type="text" name="UrlHinh" id="UrlHinh" class="txt" value="{UrlHinh}"/>
<textarea name="MoTa" id="MoTa" class="txt" />{MoTa}</textarea>
<input type="radio" name="AnHien" id="0" value="0" {An}/>Ẩn
<input type="radio" name="AnHien" id="1" value="1" {Hien}/>Hiện
5
Hàm lấy loại SP (layloaiSP.php)
<?php require_once("dbconn.php"); ?>

<?php
$idCL = $_GET['idCL']; settype($idCL, "int");
$sql = "SELECT idLoai, TenLoai FROM loaisp WHERE idCL=$idCL ORDER BY TenLoai";
$loai = mysql_query($sql) or die(mysql_error());
?>

<?php while ($row_loai = mysql_fetch_assoc($loai))


{ ?>
<option value="<?php echo $row_loai['idLoai'];?>">
<?php echo $row_loai['TenLoai'];?>
</option>
<?php } ?>

Trang phân loại SP

Code đầu trang


<?php require_once("dbconn.php");
$idLoai=$_GET['idLoai']; settype($idLoai,"int"); ?>
Code sau tab Body
<?php
$sql="select idSP, TenSP, UrlHinh, Gia, TenLoai, TenCL
from sanpham,loaisp,chungloai
where sanpham.AnHien=1 and sanpham.idLoai=$idLoai and sanpham.idLoai=loaisp.idLoai and
sanpham.idCL=chungloai.idCL
order by idSP DESC";
$sanpham=mysql_query($sql)or die (mysql_error());
$row_sanpham=mysql_fetch_assoc($sanpham);
$dem=0;
?>
Code toàn bảng
<table cellpadding=0 cellspacing=0 id="tbl">
<caption> <?php echo $row_sanpham['TenCL']." / ".$row_sanpham['TenLoai'];?></caption>
6
<tr>
<? do{ ?>
<td id="sanpham">
<a href="home.php?p=chitiet&idSP=<? echo $row_sanpham['idSP']?>">
<div align="center"><img src="<? echo $row_sanpham['UrlHinh']?>" width="100px" height="100px" border="0">
</div>
</a>
<div id="TenSP"><b><i><? echo $row_sanpham['TenSP'];?></i></b></div>
<div id="Gia"><? $dem++; echo number_format($row_sanpham['Gia'])?> VNĐ</div>
<div align="center" id="button">
<a href="#" idSP="<?php echo $row_sanpham['idSP']; ?>" class="dathang"><img src="image/dathang.gif"</a>
</div>
</td>
<? if($dem%3==0)echo "</tr><tr>"?>
<? }while($row_sanpham=mysql_fetch_assoc($sanpham));?>
</tr>
</table>

Trang sản phẩm mới

Code đầu trang


<?php require_once("dbconn.php"); ?>
Code sau tab Body
<?php
$sql=mysql_query("select idSP, TenSP, UrlHinh, Gia
from sanpham
where AnHien=1 and SoLuongTonKho>0
order by idSP DESC
limit 0, 12") or die (mysql_error());
$dem=0;
?>
Code toàn bảng

7
<table cellpadding=0 cellspacing=0 id="tbl" align="center">
<caption>SẢN PHẨM MỚI</caption>
<tr>
<? while($sanpham=mysql_fetch_assoc($sql))
{
?>
<td id="sanpham">
<div><img src="image/new.gif" /></div>
<a href="home.php?p=chitiet&idSP=<? echo $sanpham['idSP']?>">
<div align="center"><img src="<? echo $sanpham['UrlHinh']?>" width="100px" height="100px" border="0"></div>
</a>
<div id="TenSP"><b><i><? echo $sanpham['TenSP'];?></i></b></div>
<div id="Gia"><? $dem++; echo number_format($sanpham['Gia'])?> VNĐ</div>
<div align="center" id="button">
<a href="#" idSP="<?php echo $sanpham['idSP']; ?>" class="dathang"><img src="image/dathang.gif"/></a>
</div>
</td>
<? if($dem%3==0)echo "</tr><tr>"?>
<? }?>
</tr>
</table>

8
Trang tìm kiếm SP
Code trong tab head
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#cbochungloai").change(function(){
var idCL=$(this).val();
$("#cboloai").load("layloaisp.php?idCL="+ idCL);
});
});
Code cbochungloai
<label for="cbochungloai"></label>
<select name="cbochungloai" id="cbochungloai" >
<option value="-1" >[-- Chủng loại sản phẩm --] </option>
<? $sql=mysql_query("select idCL, TenCL from chungloai");
while($cloai=mysql_fetch_assoc($sql))
{
$idCL=$cloai['idCL'];
$TenCL=$cloai['TenCL'];
?>
<option value="<? echo $idCL ?>"><? echo $TenCL ?> </option>
<? }?>
</select>
Code cboLoai
<label for="cboloai"></label>
<select name="cboloai" id="cboloai" >
<option value="-1"> [-Loai san pham-] </option>
</select>
Code cboGia
<label for="cboGiaBD"></label>
<select name="cboGiaBD" id="cboGiaBD" >
<option value="-1"> [-Gia bat dau-]</option>
<option value="200000"> 200.000 vnd</option>
</select>

Trang kết quả tìm kiếm SP


<? require_once "dbconn.php";
if(isset($_GET['btnTimKiem'])){
$TenSP=$_GET['txtTenSP'];

9
settype($idLoai,"int");settype($idCL,"int");settype($giabatdau,"int");settype($giaketthuc,"int");
$TenSP=trim(strip_tags($TenSP));
if (get_magic_quotes_gpc()==false)
$TenSP = mysql_real_escape_string($TenSP);
$sql="select idSP, TenSP, UrlHinh, Gia
from sanpham
where ((TenSP like '%$TenSP%' OR '$TenSP'='Tên sản phẩm') and
(idLoai = $idLoai OR $idLoai=-1) and
(idCL = $idCL OR $idCL=-1) and
(Gia >$giabatdau or $giabatdau=-1) and
(Gia < $giaketthuc or $giaketthuc=-1))";
$sp=mysql_query($sql) or die(mysql_error());
$dem=0;
if(mysql_num_rows($sp)!=0) {
?>
<form id="form1" name="form1" method="post" action="">
<table> <caption>KẾT QUẢ TÌM KIẾM</caption>
<tr>
<? while($row_sp=mysql_fetch_assoc($sp)) { ?>
<td id="sanpham">
<!-- <a href="home.php?p=chitiet&idSP=<? echo $row_sp['idSP']?>"> -->
<div align="center">
<img src="<? echo $row_sp['UrlHinh']?>" width="120px" height="120px" border="1">
</div>
</a>
<div align="center" id="tenSp"><b><i><? echo $row_sp['TenSP'];?></i></b></div>
<div align="center">Giá:<? $dem++; echo number_format($row_sp['Gia']);?>VNĐ;</div>
<div align="center" id="button">
<a href="#" idSP="<?php echo $row_sp['idSP']; ?>" class="dathang"><img src="image/dathang.gif"</a>
</div>
</td>
<? if($dem%3==0)echo "</tr><tr>"?>
<? } ?>
</tr>
</table>
<? }
else ?>
<div>Không tìm thấy sản phẩm</div>
<? } ?>

Code Thêm Sản phẩm


10
Code đầu trang
<?php require_once("dbconn.php");
if (isset($_POST['btnThem'])==true) {
//Tiếp nhận dữ liệu từ form
$TenSP = $_POST['TenSP'];
$Gia = $_POST['Gia'];
$UrlHinh = $_POST['UrlHinh'];
$AnHien = $_POST['AnHien'];
$SoLuongTonKho = $_POST['Soluong'];
$idCL = $_POST['idCL'];
$idLoai = $_POST['idLoai'];
$MoTa = $_POST['MoTa'];
$Ghichu = $_POST['Ghichu'];
//Kiểm tra dữ liệu đã nhận
settype($AnHien,"int");
settype($Gia,"int");
settype($idCL,"int");
settype($idLoai,"int");
settype($SoLuongTonKho,"int");
$TenSP = trim(strip_tags($TenSP));
$MoTa = trim(strip_tags($MoTa));
$Ghichu = trim(strip_tags($Ghichu));
$UrlHinh = trim(strip_tags($UrlHinh));
if (get_magic_quotes_gpc()== false) {
$TenSP = mysql_real_escape_string($TenSP);
$MoTa = mysql_real_escape_string($MoTa);
$Ghichu = mysql_real_escape_string($Ghichu);
$UrlHinh = mysql_real_escape_string($UrlHinh); }
$Ngay=date("Y-m-d");
//chèn vào db
$sql="INSERT INTO sanpham (TenSP, MoTa, UrlHinh, Gia, SoLuongTonKho, idCL, idLoai, AnHien, GhiChu, NgayCapNhat)
VALUES ('$TenSP','$MoTa','$UrlHinh',$Gia, $SoLuongTonKho, $idCL, $idLoai, $AnHien,'$GhiChu','$Ngay')";
mysql_query($sql) or die(mysql_error());
header("location:index.php");
}?>

Code trong head


<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#idCL").change(function() {
var idCL=$(this).val();
$("#idLoai").load("layloaisp.php?idCL="+ idCL);
});
});
</script>

Code sau body


<?php $sql="SELECT idCL, TenCL FROM chungloai ORDER BY TenCL ASC";

11
$CL=mysql_query($sql) or die(mysql_error()); ?>

Code combo chủng loại


<?php while ($row_CL = mysql_fetch_assoc($CL)) { ?>
<option value="<?php echo $row_CL['idCL'];?>"> <?php echo $row_CL['TenCL'];?> </option>
<?php } ?>

12

You might also like