My pagination method is not intuitive...
Posted: Thu May 14, 2009 7:13 am
I finally managed to make a pagination script. I used the numbered links section from a php class and it figures it wasn't really that intuitive.
you see, the links below are like this
First | Previous | 1 2 3 4 5 | Next | Last
Now the problem is that when I click the last number link (5 in our case) it should display the next 5 results, but it doesn't do it, instead I have to click 'Next' in order for it to do so. Also this happens when going backwards.
Any help?
my pagination script:
index.php for testing
you see, the links below are like this
First | Previous | 1 2 3 4 5 | Next | Last
Now the problem is that when I click the last number link (5 in our case) it should display the next 5 results, but it doesn't do it, instead I have to click 'Next' in order for it to do so. Also this happens when going backwards.
Any help?
my pagination script:
Code: Select all
<?php
/* PAGINATION */
//usage: list($paginated_query,$max_pages)=paginate_query($db_connection,$news_query,5)
/* global variables */
global $section;
global $action;
global $keywords;
$script_fname = htmlspecialchars($_SERVER['PHP_SELF']);
$get_url = htmlspecialchars($script_fname.'?section='.$section.'&action='.$action.'&keywords='.$keywords);
/* paginate a query */
function paginate_query($db_connection,$sql_query,$rows_per_page)
{
global $page;
if ($rows_per_page <= 0){$rows_per_page=5;}
$page=intval($page);
//total rows
$sql_result=mysql_query($sql_query) or die ('Error in Query: ' . mysql_error());
$total_rows = mysql_num_rows($sql_result);
//get max pages
$max_pages = ceil($total_rows/$rows_per_page);
//Check the page value just in case someone is trying to input an aribitrary value
if($page > $max_pages || $page <= 0) {$page = 1;}
//calculate offset
$offset = $rows_per_page * ($page-1);
$paginated_query = mysql_query($sql_query." LIMIT $offset, $rows_per_page") or die ('Error in Query: ' . mysql_error());
return array ($paginated_query, $max_pages);
}
/* create the first link */
function pagelink_first($enabled_label,$disabled_label)
{
global $page;
global $get_url;
if($page == 1){return $disabled_label;}else{return '<a href="'.$get_url.'&page=1">'.$enabled_label.'</a>';}
}
/* create the last link */
function pagelink_last($enabled_label,$disabled_label)
{
global $get_url;
global $max_pages;
global $page;
if($page == $max_pages){return $disabled_label;}else{return '<a href="'.$get_url.'&page='.$max_pages.'">'.$enabled_label.'</a>';}
}
/* create the next link */
function pagelink_next($enabled_label,$disabled_label) {
global $get_url;
global $max_pages;
global $page;
if($page < $max_pages) {return '<a href="'.$get_url.'&page='.($page+1).'">'.$enabled_label.'</a>';}
else {return '<a>'.$disabled_label.'</a>';}
}
/* create the previous link */
function pagelink_previous($enabled_label,$disabled_label) {
global $get_url;
global $max_pages;
global $page;
if($page > 1) {return '<a href="'.$get_url.'&page='.($page-1).'">'.$enabled_label.'</a>';}
else {return '<a>'.$disabled_label.'</a>';}
}
/* create the page number links */
function pagelink_numbers($numlinks_per_page) {
global $get_url;
global $max_pages;
global $page;
if ($numlinks_per_page <= 0) {$numlinks_per_page=5;}
for($i=1;$i<=$max_pages;$i+=$numlinks_per_page) {
if($page >= $i) {
$start = $i;
}
}
if($max_pages > $numlinks_per_page) {
$end = $start+$numlinks_per_page;
if($end > $max_pages) $end = $max_pages+1;
}
else {
$end = $max_pages;
}
$links = '';
for( $i=$start ; $i<$end ; $i++) {
if($i == $page) {
$links .= ' <a>'.$i.'</a> ';
}
else {
$links .= ' <a href="'.$get_url.'&page='.$i.'">'.$i.'</a> ';
}
}
return $links;
}
/* show the page navigation */
function show_pages()
{
echo pagelink_first('First','First').' | '.pagelink_previous('Previous','Previous').' | '.pagelink_numbers(5).' | '.pagelink_next('Next','Next').' | '.pagelink_last('Last','Last');
}
/* count the page number */
function count_pages()
{
global $max_pages;
global $page;
echo 'Page '.$page.' of '.$max_pages;
}
?>Code: Select all
<?php
ob_start();
/* REQUIRE EXTERNAL SCRIPTS */
require_once('database-connect.php');
require_once('paginate.php');
$page=$_GET['page'];
$news_query = "SELECT * FROM articles";
$news_result = mysql_query($news_query) or die('Error in Query: ' . mysql_error());
list($paginated_query,$max_pages)=paginate_query($db_connection,$news_query,5);
while($row = mysql_fetch_array($paginated_query))
{
$get_id=$row['id'];
$get_title=$row['title'];
$get_description=$row['description'];
echo 'ID> '.$get_id.' title> '.$get_title.' desc> '.$get_description.'<br/>';
}
show_pages();
echo "<br/>";
count_pages();
mysql_close();
ob_end_flush();
?>