Mã nguồn php giúp bạn phân trang trong web php .Đặt biệt function này có thể cuộn giống như google
Function phân trang
CODE function pagination($self, $numofpages, $page_num)
{
if ($numofpages > '1' )
{
$range =4;
$range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
$range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
$page_min = $page_num- $range_min;
$page_max = $page_num+ $range_max;
$page_min = ($page_min < 1) ? 1 : $page_min;
$page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
if ($page_max > $numofpages)
{
$page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
$page_max = $numofpages;
}
$page_min = ($page_min < 1) ? 1 : $page_min;
if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
$page_pagination .= '<a title="First" href="'.$self.'page=1">First</a> ';
}
if ($page_num != 1) {
$page_pagination .= '<a href="'.$self.'page='.($page_num-1). '"><<</a> ';
}
for ($i = $page_min;$i <= $page_max;$i++) {
if ($i == $page_num)
$page_pagination .= "<span class='current'>" . $i . '</span> ';
else
$page_pagination.= '<a href="'.$self.'page='.$i. '">'.$i.'</a> ';
}
if ($page_num < $numofpages) {
$page_pagination.= ' <a href="'.$self.'page='.($page_num + 1) . '">>></a>';
}
if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
$page_pagination .= ' <a title="Last" href="'.$self.'page='.$numofpages. '">Last</a> ';
}
$page_pagination="<span class='sotrang'>Trang ".$page_num."/".$numofpages."</span>".$page_pagination;
}
return $page_pagination;
}
Css style
CODE div.pagination {
padding:5px;
margin:5px;
text-align:center;
float:left;
font-size:10px;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin-right: 2px;
border: 1px solid #ddd;
text-decoration: none;
color: #990000;
}
div.pagination a:hover, div.pagination a:active {
border:1px solid #ddd;
color: #fff;
background-color: #990000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin-right: 2px;
border: 1px solid #ddd;
font-weight: bold;
background-color: #990000;
color: #FFF;
}
div.pagination span.sotrang {
padding: 2px 5px 2px 5px;
margin-right: 2px;
border: 1px solid #ddd;
text-decoration: none;
color: #990000;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin-right: 2px;
border: 1px solid #ddd;
color: #ccc;
}
Cách dùng
CODE $pp =10; // số mẫu tin hiển thị
$p_now = intval($_GET ['p']);
$total = GetNumRow('products_id','products',$where); // Đếm số mẫu tin trong database
$numofpages = $total / $pp;
if ($p_now <= 0)
{
$page = 1;
}
else
{
if($p_now <= ceil($numofpages))
$page = $p_now;
else
$page = 1;
}
$limitvalue = $page * $pp - ($pp);
echo pagination($linkS.'',ceil ( $numofpages ), $page);
Chúc các bạn thành công |
Thêm vào trang Google +