Đây là 1 hàm phân trang chia thành từng đoạn, TG đang sử dụng và post lên đây, anh em nào có nhu cầu phân trang thì sử dụng:
<?php
function divPage($total = 0,$currentPage = 0,$div = 5,$rows = 10){
if(!$total || !$rows || !$div || $total<=$rows) return false;
$nPage = floor($total/$rows) + (($total%$rows)?1:0);
$nDiv = floor($nPage/$div) + (($nPage%$div)?1:0);
$currentDiv = floor($currentPage/$div) ;
$sPage = '';
if($currentDiv) {
$sPage .= '<a href="./?p=0"><<</a> ';
$sPage .= '<a href="./?p='.($currentDiv*$div - 1).'"><</a> ';
}
$count =($nPage<=($currentDiv+1)*$div)?($nPage-$currentDiv*$div):$div;
for($i=0;$i<$count;$i++){
$page = ($currentDiv*$div + $i);
$sPage .= '<a href="./?p='.($currentDiv*$div + $i ).'" '.(($page==$currentPage)?'class="current"':'').'>'.($page+1).'</a> ';
}
if($currentDiv < $nDiv - 1){
$sPage .= '<a href="./?p='.(($currentDiv+1)*$div + 1 ).'">></a> ';
$sPage .= '<a href="./?p='.(($nDiv-1)*$div + 1 ).'">>></a>';
}
?>
Giải thích các thông số:
$total: tổng số mẫu tin
$currentPage: trang hiện hành
$div: số trang trong 1 đoạn
$rows: số dòng trên 1 trang
Cách dùng:
<?php
$p = $_GET['p'];// currentPage
$rows = 10; // số record trên mỗi trang
$div = 5; // số trang trên 1 phân đoạn
$sql = "SELECT COUNT(*) AS total FROM <table> WHERE <dieu_kine>";
//fetch dữ liệu lấy giá trị của total, tổng số record với điều kiện là <dieu_kien>, ta được biến $total;
//lấy dữ liệu cho trang $p
$start = $p*$rows;
$sql = "SELECT * FROM <table> WHERE <dieu_kine> LIMIT $start,$rows";
// hiển thị dữ liệu
// in phân trang
print divPage($total,$p,$div,$rows)
?>
Chúc thành công!
Tác giả:TG
Webmaster
http://phpbasic.com

