Php pagination
From w3cyberlearnings
Contents |
PHP function mypaging_bar
This function generate pagination for all types of database or data.
Syntax mypaging_bar
- $totalcount: Total Records
- $page: current page (first page start with 0)
- $perpage: display record per page
- $baseurl: the URL (/page.php')
- $pagevar: assign the variable for the URL (/page.php?page=..)
- $nocurr: false
- $return: return or echo (when set to true it will return so that we can assign to another variable)
- $maxdisplay: the maximum display for records
function mypaging_bar( $totalcount, $page, $perpage, $baseurl, $pagevar='page', $nocurr=false, $return=false, $maxdisplay=18) { $output = ''; if ($totalcount > $perpage) { $output .= '<div class="paging">'; $output .= 'page' . ':'; if ($page > 0) { $pagenum = $page - 1; $output .= ' (<a class="previous" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . 'previous' . '</a>) '; } if ($perpage > 0) { $lastpage = ceil($totalcount / $perpage); } else { $lastpage = 1; } if ($page > $maxdisplay - 3) { $startpage = $page - floor($maxdisplay / 2); $output .= ' <a href="' . $baseurl . $pagevar . '=0">1</a> ...'; } else { $startpage = 0; } $currpage = $startpage; $displaycount = $displaypage = 0; while ($displaycount < $maxdisplay and $currpage < $lastpage) { $displaypage = $currpage + 1; if ($page == $currpage && empty($nocurr)) { $output .= ' ' . $displaypage; } else { $output .= ' <a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $displaypage . '</a>'; } $displaycount++; $currpage++; } if ($currpage < $lastpage) { $lastpageactual = $lastpage - 1; $output .= ' ...<a href="' . $baseurl . $pagevar . '=' . $lastpageactual . '">' . $lastpage . '</a> '; } $pagenum = $page + 1; if ($pagenum != $displaypage) { $output .= ' (<a class="next" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . 'next' . '</a>)'; } $output .= '</div>'; } if ($return) { return $output; } echo $output; return true; }
Example 1
$totalcounts = 200; $page = 1; $perpage = 5; $baseurl = 'content.php?act=usesrs&'; $pagevar = 'page'; $nocurr = false; mypaging_bar($totalcounts, $page, $perpage, $baseurl, $pagevar = 'page', $nocurr = false);