Phân trang trong PHP 2007-07-12 12:04:24
Đâ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!
Tra loi 24 comment(s) 2007-07-12 12:04:24
toiyeuphp 2007-07-12 12:28:11
Tra loi
kirk 2007-07-13 13:38:04
<?
function page_div($link, $offset, $numofpages, $page) {
$numofpages = ceil($numofpages);
$pagesstart = ceil($page-$offset);
$pagesend = ceil($page+$offset);
if ($page != "1" && ceil($numofpages) != "0")
{
echo str_replace('%d_pg', ceil($page-1), "<a href="$link"><b>Pre</b></a> ");
}
for($i = 1; $i <= $numofpages; $i++)
{
if ($pagesstart <= $i && $pagesend >= $i)
{
if ($i == $page)
{
echo "<b>[$i]</b> ";
}
else
{
echo str_replace('%d_pg', "$i", '<a href="'.$link.'"><b>'.$i.'</b></a> ');
}
}
}
if (ceil($numofpages) == "0")
{
echo "[$i]";
}
if ($page != ceil($numofpages) && ceil($numofpages) != "0")
{
echo str_replace('%d_pg', ceil($page+1), '<a href="'.$link.'"><b>Next</b></a>');
}
}
?>
Cách dùng:
<?
$pp = "10";
$p_now=$_GET['p'];
$total = mysql_result(mysql_query("SELECT COUNT(id) FROM <table>"),0);
$numofpages = $total / $pp;
if (!$p_now) {$page = 1;}
else {$page = $p_now;}
$limitvalue = $page * $pp - ($pp);
//lấy dữ liệu cho trang
$sql="SELECT * FROM <table> WHERE <dieu_kine> LIMIT $limitvalue, $pp";
// hiển thị dữ liệu
// in phân trang
echo 'Pages: '.ceil($numofpages).'<br>';
page_div("?action=donow&p=%d_pg", "10", ceil($numofpages), $page);
?>
Thử hàm này xem sao.
Tra loi
Mydream 2007-12-26 02:49:38
Tớ chạy đoạn mã code theo hàm divpage() nhưng khi hiển thị trên trang sản phẩm của tớ, thì kết quả là trang hiển thị list tất cả các sản phẩm trên 1 trang màn hình, ý tớ là nếu tứ cvó 50 sản phẩm thì nó hiển thị hết 50 sản phẩm trên cùng một trang TG ạ
Tra loi
Mydream 2007-12-26 03:48:08
Khi tớ chạy hàmg phân trang của bạn như đã posted, trình duyệt báo lỗi ở dòng lênh đầu tiên của function: echo str_replace('%d_pg', ceil($page-1), "<a href="$link"><b>Pre</b></a> ");
Lỗi Warning: Biến không được định nghĩa trong hàm str_replace() ?. Vậy phải gán các tham số hàm như thế nào thì đúng ?.
Tra loi
TG 2007-12-26 04:36:28
Tra loi
NguoiMoi 2008-01-01 02:33:05
vd:Nếu trang hiện tại là 1 thì nó hiện như vầy
[1]
2 3 4 Next
vd:Nếu trang hiện tại là 2 thì nó hiện như vầy
1 [2]
3 4 Next
vd:Nếu trang hiện tại là 2 thì nó hiện như vầy
1 2 [3]
4 Next
.....
sao nó không nằm trên 1 hàng mà lại xuống hàng???
===> không được đẹp:D
Tra loi
GnuhNguyen 2008-01-01 03:02:50
1 2 3 4 5 nên muốn sang trang 6, 7 , 8 thì bắt buộc phải vào trang 5. Tớ thì hay để dạng select đổ xuống ^^
Tra loi
web20vn.com 2008-01-02 09:25:15
Tra loi
GnuhNguyen 2008-02-17 09:06:14
Tra loi
gaulucky92 2008-02-21 10:59:31
$limit = 0;
$new_post_per_page = "10"; //Số records trên 1 trang
$qr = mysql_query("SELECT * FROM tag WHERE tag = \"$tag\"") or die (mysql_error());
$check = mysql_num_rows($qr);
$split = $check/$new_post_per_page;
$countpg = ceil($split);
if (isset($_GET['page']) && $_GET['page'] > '1'){
$page = $_GET['page'];
$limit = ($page-1)*$new_post_per_page;
$new_post_per_page <= $new_post_per_page+$new_post_per_page;}
else{$page = 1;}
// Show!!
$b = 1;
$pages = array();
for ($i=$page-$page+1;$i<=$page-1;$i++)
{$pages[$b] = "[<A href=\"index.php?act=default&page=$i\">$i</a>] ";$b++; } // Previous Page(s)
$pages[$b] = "<B>$page</B> ";$b++; // Current Page
if ($countpg > $page) // Next Page(s)
{for ($a=$page+1;$a<=$countpg;$a++) {$pages[$b] = "[<A href=\"index.php?act=default&page=$a\">$a</a>] ";$b++; }}
$pages[] = '';
// [END] Code for showing page numbers
Cũng phân số trang, nhưng hơi bất tiện là dù chỉ có 1 trang nhưng nó vẫn hiện số trang ra ("Trang 1" - giống như phpBB:"Trang x trên tổng số y trang"), và số trang cứ thế tăng dần ko rút gọn lại như forum được (VD có 100 trang thì nó hiện ra đủ... 100 số T_T), có ai biết cách sửa chỉ mình với!
Tra loi
GnuhNguyen 2008-02-23 04:10:14
ví dụ:
<form action=index.php>
<input type=hidden name=act value=default>
<select name=page>
//////
<option value='$i'>
nếu là Current Page thì thêm selected cho cái option này
//////
</select>
<input type=submit class=button value='GO'>
</form>
Tớ chỉ nói cách viết thôi, còn viết thế nào, tùy vào code bạn mà sửa lại ^^.
Tra loi
gaulucky92 2008-02-23 09:13:50
Tra loi
Ngọc Minh 2008-02-28 11:02:10
Tra loi
gaulucky92 2008-02-29 11:49:04
Work 100% ^^
Tra loi
Ngọc Minh 2008-03-01 04:16:38
Em đang tập sài nó,và tập code luôn!Chứ không kịp theo mấy anh luôn đó :)
Tra loi
liukang 2008-12-24 11:22:58
Mình thử test code của TG, sao nó báo lổi này hoài ta?
Đây là code test.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>';
}
if(isset($_GET['p']))
{
$p = $_GET['p'];// currentPage
}
else
{
$p=0;
}
$rows = 3; // số record trên mỗi trang
$div = 5; // số trang trên 1 phân đoạn
$total=100; // vi du total record lay ve la 100
echo "Test hien thi du lieu";
//$start = $p * $rows;
print divPage($total,$p,$div,$rows);
?>
Tra loi
gaulucky92 2008-12-24 11:56:58
hàm divPage() chưa có dấu } kết thúc
Tra loi
easyphuoc 2008-12-30 06:40:22
Tra loi
NữThần 2008-12-30 06:49:23
Mấy bạn nên học những cái cơ bản nhất và làm sao sử dụng chúng một cách linh động thì hay hơn là học các FW hay TL.
Tra loi
Linh 2009-03-04 01:36:16
Tra loi
downloadsbank.com 2009-07-26 12:32:08
Tra loi
phuongphp 2009-07-28 05:37:05
download
Tra loi
luuson@gmail.com 2009-09-11 06:32:33
cảm ơn các bạn đã Share.
Đây là đoạn code mình đã FIX lại và Design xin Share cho ai cần.
<style>
#pagination-digg li { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }
#pagination-digg a { border:solid 1px #9aafe5; margin-right:2px; }
#pagination-digg .active { background:#2e6ab1; color:#FFFFFF; font-weight:bold; display:block; float:left; padding:4px 6px; /* savers */ margin-right:2px; }
#pagination-digg a:link,
#pagination-digg a:visited { color:#0e509e; display:block; float:left; padding:3px 6px; text-decoration:none; }
#pagination-digg a:hover { border:solid 1px #0e509e; }
</style>
<?php
function page_div($link, $offset, $numofpages, $page) {
$numofpages = ceil($numofpages);
$pagesstart = ceil($page-$offset);
$pagesend = ceil($page+$offset);
if ($page != "1" && ceil($numofpages) != "0")
{
echo str_replace('%d_pg', ceil($page-1), " <a href=".$link." ><b>< Pre</b></a> ");
}
for($i = 1; $i <= $numofpages; $i++)
{
if ($pagesstart <= $i && $pagesend >= $i)
{
if ($i == $page)
{
echo "<b class=active>$i</b> ";
}
else
{
echo str_replace('%d_pg', "$i", '<a href="'.$link.'"><b>'.$i.'</b></a> ');
}
}
}
if (ceil($numofpages) == "0")
{
echo "$i";
}
if ($page != ceil($numofpages) && ceil($numofpages) != "0")
{
echo str_replace('%d_pg', ceil($page+1), '<a href="'.$link.'"><b> Next</b></a>');
}
}
include('../connections.php');
$pp = "10";
$p_now=$_GET['p'];
$q2 = "SELECT count(*) as tot FROM cn05b_sanpham";
$s2 = mysql_query($q2,$link);
$r2 = @mysql_fetch_array($s2);
$numofpages = $r2['tot'] / $pp;
if (!$p_now) {$page = 1;}
else {$page = $p_now;}
$limitvalue = $page * $pp - ($pp);
//lấy dữ liệu cho trang
$sql="SELECT * FROM cn05b_sanpham LIMIT $limitvalue, $pp";
// hiển thị dữ liệu
?>
<div id=pagination-digg> <?
// in phân trang
echo 'Pages: '.ceil($numofpages).'<br>';
page_div("?action=donow&p=%d_pg", "5", ceil($numofpages), $page);
?> </div>
Tra loi
slopesky@gmail.com 2009-11-06 04:48:36
Tra loi
Y kien