10/10/2018, 10:29
Cần Giúp đỡ về cách phân trang trong PHP
Các Bác vui lòng chỉ giúp em đoạn code này, Đây là đoạn code em sưu tầm được, trong trang web em nó phân ra 15 hàng, sang hàng thứ 16 thì nó chuyển qua trang khác. Bây giờ em muốn sửa lại cho nó đếm được 100 hàng thì mới qua trang 2 thì làm thế nào? Mong các pro giúp đỡ. Em mới tập tành mò php.
/*
$pageVars[0] => sql table
$pageVars[1] => Lien ket chuyen trang
$pageVars[2] => So ket qua hien tren 1 trang
$pageVars[3] => Lay gia tri tu bien $start
$pageVars[4] => sql where
*/
function pagination($pageVars, $pageCss = array('pagecurrent', 'pagelink', 'pagelinklast'), $pagePrevNext = '«|»')
{
global $db, $lang;
$result = $db->sql_query('SELECT COUNT(*) AS totalrows FROM ' . $pageVars[0] . (!empty($pageVars[4]) ? ' ' . $pageVars[4] : '));
$dataCount = $db->sql_fetchrow();
$db->sql_freeresult();
$pageString = ';
$totalPages = ceil($dataCount['totalrows'] / $pageVars[2]);
if ( $totalPages > 1 )
{
$onPage = floor($pageVars[3] / $pageVars[2]) + 1;
if ( $totalPages > 10 )
{
$pageMax = ( $totalPages > 3 ) ? 3 : $totalPages;
for($i = 1; $i < $pageMax + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1]. "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $pageMax )
{
$pageString .= " ";
}
}
if ( $totalPages > 3 )
{
if ( $onPage > 1 && $onPage < $totalPages )
{
$pageString .= ( $onPage > 5 ) ? ' ... ' : ' ';
$pageMin = ( $onPage > 4 ) ? $onPage : 5;
$pageMax = ( $onPage < $totalPages - 4 ) ? $onPage : $totalPages - 4;
for($i = $pageMin - 1; $i < $pageMax + 2; $i++)
{
$pageString .= ($i == $onPage) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $pageMax + 1 )
{
$pageString .= ' ';
}
}
$pageString .= ( $onPage < $totalPages - 4 ) ? ' ... ' : ' ';
}
else
{
$pageString .= ' ... ';
}
for($i = $totalPages - 2; $i < $totalPages + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $totalPages )
{
$pageString .= " ";
}
}
}
}
else
{
for($i = 1; $i < $totalPages + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $totalPages )
{
$pageString .= ' ';
}
}
}
$pageNav = explode("|", $pagePrevNext);
if ( $onPage > 1 )
{
$pageString = ' <span class="'. $pageCss[2] .'"><a title="'. $lang['general_prevpage'] .'" href="' . ($pageVars[1] . "&start=" . ( ( $onPage - 2 ) * $pageVars[2] ) ) . '"><font style="font-size:13px">' . $pageNav[0] . '</font></a></span>  ' . $pageString;
}
if ( $onPage < $totalPages )
{
$pageString .= ' <span class="'. $pageCss[2] .'"><a title="Tiếp theo" href="' . ($pageVars[1] . "&start=" . ( $onPage * $pageVars[2] ) ) . '"><font style="font-size:13px">' . $pageNav[1] . '</font></a></span>';
}
$pageString = ' Trang: ' . $pageString;
}
return array('page' => $pageString, 'total' => $dataCount['totalrows']);
}
$pageVars[0] => sql table
$pageVars[1] => Lien ket chuyen trang
$pageVars[2] => So ket qua hien tren 1 trang
$pageVars[3] => Lay gia tri tu bien $start
$pageVars[4] => sql where
*/
function pagination($pageVars, $pageCss = array('pagecurrent', 'pagelink', 'pagelinklast'), $pagePrevNext = '«|»')
{
global $db, $lang;
$result = $db->sql_query('SELECT COUNT(*) AS totalrows FROM ' . $pageVars[0] . (!empty($pageVars[4]) ? ' ' . $pageVars[4] : '));
$dataCount = $db->sql_fetchrow();
$db->sql_freeresult();
$pageString = ';
$totalPages = ceil($dataCount['totalrows'] / $pageVars[2]);
if ( $totalPages > 1 )
{
$onPage = floor($pageVars[3] / $pageVars[2]) + 1;
if ( $totalPages > 10 )
{
$pageMax = ( $totalPages > 3 ) ? 3 : $totalPages;
for($i = 1; $i < $pageMax + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1]. "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $pageMax )
{
$pageString .= " ";
}
}
if ( $totalPages > 3 )
{
if ( $onPage > 1 && $onPage < $totalPages )
{
$pageString .= ( $onPage > 5 ) ? ' ... ' : ' ';
$pageMin = ( $onPage > 4 ) ? $onPage : 5;
$pageMax = ( $onPage < $totalPages - 4 ) ? $onPage : $totalPages - 4;
for($i = $pageMin - 1; $i < $pageMax + 2; $i++)
{
$pageString .= ($i == $onPage) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $pageMax + 1 )
{
$pageString .= ' ';
}
}
$pageString .= ( $onPage < $totalPages - 4 ) ? ' ... ' : ' ';
}
else
{
$pageString .= ' ... ';
}
for($i = $totalPages - 2; $i < $totalPages + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $totalPages )
{
$pageString .= " ";
}
}
}
}
else
{
for($i = 1; $i < $totalPages + 1; $i++)
{
$pageString .= ( $i == $onPage ) ? '<span class="'. $pageCss[0] .'">' . $i . '</span>' : '<span class="'. $pageCss[1] .'"><a href="' . ($pageVars[1] . "&start=" . ( ( $i - 1 ) * $pageVars[2] ) ) . '">' . $i . '</a></span>';
if ( $i < $totalPages )
{
$pageString .= ' ';
}
}
}
$pageNav = explode("|", $pagePrevNext);
if ( $onPage > 1 )
{
$pageString = ' <span class="'. $pageCss[2] .'"><a title="'. $lang['general_prevpage'] .'" href="' . ($pageVars[1] . "&start=" . ( ( $onPage - 2 ) * $pageVars[2] ) ) . '"><font style="font-size:13px">' . $pageNav[0] . '</font></a></span>  ' . $pageString;
}
if ( $onPage < $totalPages )
{
$pageString .= ' <span class="'. $pageCss[2] .'"><a title="Tiếp theo" href="' . ($pageVars[1] . "&start=" . ( $onPage * $pageVars[2] ) ) . '"><font style="font-size:13px">' . $pageNav[1] . '</font></a></span>';
}
$pageString = ' Trang: ' . $pageString;
}
return array('page' => $pageString, 'total' => $dataCount['totalrows']);
}
Bài liên quan
<?php
/*
+ $base_url : the url to get page
ex : http://site.com/index.php?view=product
will translation to http://site.com/index.php?view=product&page=1 ...
+ $total : total of product
+ $current : the current page
+ $limit : max number product in a page
+ $num_digit : the number of digit will be display
ex : num_digit = 4 , when echo $html : ...2 3 4 5 ***91;6***93; 7 8 9 10...
*/
function get_num_page($base_url,$total,$current,$limit,$num_digit){
$total_page=ceil($total/$limit);
if ($total>0){
$pre_page= ($current >1) ? $current-1 : $current ;
$next_page= ($current < $total_page ) ? $current+1 : $total_page ;
$final_page=$total_page ;
if ($total_page>$num_digit*2+1){
if ($num_digit+1>=$current){
$num_start=1;
$num_end=$num_digit*2+1;
$html_start="";
$html_end=" ... ";
}elseif(($num_digit+1<$current) and ($total_page-($num_digit)>$current)){
$num_start=$current-($num_digit);
$num_end=$current+$num_digit;
$html_start=" ... ";
$html_end=" ... ";
}elseif($total_page-($num_digit)<=$current){
$num_start=$total_page-($num_digit*2);
$num_end=$total_page;
$html_start=" ... ";
$html_end="";
}
}else{
$num_start=1;
$num_end=$total_page;
$html_start="";
$html_end="";
}
$html="";
$html.="<a href=\"$base_url&page=1\"> Đầu</a> |";
$html.="<a href=\"$base_url&page=$pre_page\">Trước</a> | ";
$html.=$html_start;
for ($i=$num_start ;$i <= $num_end;$i++){
if ($i==$current){
$html.="<a href=\"$base_url&page=$i\"><b>***91;$i***93;</b></a> ";
}
else{
$html.="<a href=\"$base_url&page=$i\">$i</a> ";
}
}
$html.=$html_end;
$html.="| <a href=\"$base_url&page=$next_page\">Sau</a> |";
$html.="<a href=\"$base_url&page=$final_page\">Cuối</a>";
return $html;
}else{
return false;
}
}
?>
http://pear.php.net/package/Pager
Xem ví dụ tại http://svn.php.net/viewvc/pear/packa...20&view=markup
function generate_page($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE) {
$total_pages = ceil($num_items/$per_page);
if ($total_pages == 1) { return ''; }
@$on_page = floor($start_item / $per_page) + 1;
$page_string = '';
if($total_pages > 10) {
$init_page_max = ($total_pages > 3) ? 3 : $total_pages;
for($i = 1; $i < $init_page_max + 1; $i++) {
$page_string .= ($i == $on_page) ? '<b>'.$i.'</b>' : '<a href="'.$base_url."&page=".(($i - 1) * $per_page).'">'.$i.'</a>';
if ($i < $init_page_max) { $page_string .= ", "; }
}
if ($total_pages > 3) {
if ($on_page > 1 && $on_page < $total_pages) {
$page_string .= ($on_page > 5) ? ' ... ' : ', ';
$init_page_min = ($on_page > 4) ? $on_page : 5;
$init_page_max = ($on_page < $total_pages - 4) ? $on_page : $total_pages - 4;
for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {
$page_string .= ($i == $on_page) ? '<b>'.$i.'</b>' : '<a href="'.$base_url. "&page=" .(($i - 1) * $per_page).'">'.$i.'</a>';
if ($i < $init_page_max + 1) { $page_string .= ', '; }
}
$page_string .= ($on_page < $total_pages - 4) ? ' ... ' : ', ';
} else { $page_string .= ' ... '; }
for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {
$page_string .= ($i == $on_page) ? '<b>'.$i.'</b>' : '<a href="'.$base_url. "&page=" . (($i - 1) * $per_page).'">'.$i.'</a>';
if( $i < $total_pages ) { $page_string .= ", "; }
}
}
} else {
for($i = 1; $i < $total_pages + 1; $i++) {
$page_string .= ($i == $on_page) ? '<b>'.$i.'</b>' : '<a href="'.$base_url. "&page=" . (($i - 1) * $per_page).'">'.$i.'</a>';
if ($i < $total_pages) { $page_string .= ', '; }
}
}
if ($add_prevnext_text) {
if ($on_page > 1) {
$page_string = ' <a href="'.$base_url. "&page=" . (($on_page - 2) * $per_page).'">"Quay lai"</a> ' . $page_string;
}
if ($on_page < $total_pages) {
$page_string .= ' <a href="'.$base_url. "&page=" . ($on_page * $per_page).'">"Tiep"</a>';
}
}
$page_string = "Chuyen den trang" . $page_string;
return $page_string;
}
$url="index.php";
$all_page = 100;
$per_page = 5;
$page = 5;
$i=1;
while ($i<=$per_page)
{
echo "Dong ".$i."<br>";
$i++;
}
echo generate_page($url, $all_page, $per_page, $page);