em đang bị vướng chỗ này quá. Em đã dùng _GET['category'] để lấy id danh mục
nhưng giờ em muốn phân trang cho trang danh mục này
dùng thêm _GET['page'] thì nó bị nhảy mất ?category
giờ em muốn làm ?category=id_danhmuc&page=
cảm ơn mọi người~
Mã:
<?php
if(!$_GET['category']){
echo "khong co gi";
}
else{
$category = $_GET['category'];
//$page = $_GET['page'];
}
?>
<?php
if(!$_GET['page']){
$page = 1;
}
else{
$page = $_GET['page'];
}
$max_result = 12;
$index_row=$page * $max_result - $max_result;
?>
<?php
$sql = "SELECT * FROM sanpham WHERE id_danhmuc = '{$category}' LIMIT $index_row, $max_result";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
<li>
<a href="details.php?id=<?php echo $row['id_sanpham'] ?>"><img src="Images/<?php echo $row['img_sanpham']; ?>"/></a><br>
<?php echo $row['ten_sanpham']; ?><br>
<?php echo $row['gia_sanpham']; ?>
<span id="gia_sanpham" style="color: red;">VNĐ</span>
</li>
<?php }
?>
</ul>
<p id=paging><br></p>
<?php
// Tinh tong so sanpham co trong CSDL
$total_row = mysql_num_rows(mysql_query("SELECT * FROM sanpham WHERE id_danhmuc = '{$category}'"));
// Lam tron so trang co duoc dua vao tong so sanpham va so sanpham tren moi trang
$total_page = ceil($total_row/$max_result);
if(!$total_page)
{
echo 'Lỗi SQL: '.mysql_error();
echo "\r\n<br />";
echo 'SQL: '.$total_page;
exit;
}
// Khai bao bien danh sach so trang
$list_page = '';
// Tao nut Prev
$prev = $page - 1;
if($page > 1){
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$prev>Prev </a>";
}
// Liet ke danh sach so trang
for($i=1; $i<=$total_page; $i++){
if($page == $i){
$list_page .= "<b>$i </b>";
}
else{
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$i>".$i." </a>";
}
}
// Tao nut Next
$next = $page + 1;
if($page < $total_page){
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$next>Next</a>";
}
// Suat ra thanh phan trang
?>
<p id=paging>
<?php
echo $list_page;
?>
</p>
nhưng giờ em muốn phân trang cho trang danh mục này
dùng thêm _GET['page'] thì nó bị nhảy mất ?category
giờ em muốn làm ?category=id_danhmuc&page=
cảm ơn mọi người~
Mã:
<?php
if(!$_GET['category']){
echo "khong co gi";
}
else{
$category = $_GET['category'];
//$page = $_GET['page'];
}
?>
<?php
if(!$_GET['page']){
$page = 1;
}
else{
$page = $_GET['page'];
}
$max_result = 12;
$index_row=$page * $max_result - $max_result;
?>
<?php
$sql = "SELECT * FROM sanpham WHERE id_danhmuc = '{$category}' LIMIT $index_row, $max_result";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
<li>
<a href="details.php?id=<?php echo $row['id_sanpham'] ?>"><img src="Images/<?php echo $row['img_sanpham']; ?>"/></a><br>
<?php echo $row['ten_sanpham']; ?><br>
<?php echo $row['gia_sanpham']; ?>
<span id="gia_sanpham" style="color: red;">VNĐ</span>
</li>
<?php }
?>
</ul>
<p id=paging><br></p>
<?php
// Tinh tong so sanpham co trong CSDL
$total_row = mysql_num_rows(mysql_query("SELECT * FROM sanpham WHERE id_danhmuc = '{$category}'"));
// Lam tron so trang co duoc dua vao tong so sanpham va so sanpham tren moi trang
$total_page = ceil($total_row/$max_result);
if(!$total_page)
{
echo 'Lỗi SQL: '.mysql_error();
echo "\r\n<br />";
echo 'SQL: '.$total_page;
exit;
}
// Khai bao bien danh sach so trang
$list_page = '';
// Tao nut Prev
$prev = $page - 1;
if($page > 1){
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$prev>Prev </a>";
}
// Liet ke danh sach so trang
for($i=1; $i<=$total_page; $i++){
if($page == $i){
$list_page .= "<b>$i </b>";
}
else{
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$i>".$i." </a>";
}
}
// Tao nut Next
$next = $page + 1;
if($page < $total_page){
$list_page .= "<a href=".$_SERVER['PHP_SELF']."?page=$next>Next</a>";
}
// Suat ra thanh phan trang
?>
<p id=paging>
<?php
echo $list_page;
?>
</p>