My pagination method is not intuitive...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

My pagination method is not intuitive...

Post by Sindarin »

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:

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;
}
 
?>
index.php for testing

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();
 
?>
Last edited by Sindarin on Fri May 15, 2009 4:23 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: My pagination method is not intuitive...

Post by pickle »

Your pagination works differently than....any other pagination I've ever seen :) Typically when "5" is clicked on, the user is taken to the 5th page of results, not the next 5 results.

If I were to navigate by clicking '1', would I iterate through the results one at a time?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: My pagination method is not intuitive...

Post by Sindarin »

Typically when "5" is clicked on, the user is taken to the 5th page of results, not the next 5 results.
Well it does go to the 5th page by using offset and limit :O what do you mean? Is there another way to do it?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: My pagination method is not intuitive...

Post by pickle »

When you said:
Now the problem is that when I click the last number link (5 in our case) it should display the next 5 results
I read that to mean that if you're viewing record 27, then click on 5, it will display records 28-32.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: My pagination method is not intuitive...

Post by mattpointblank »

I assumed he meant the numbered list should then start from 5 and go up to 10?
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: My pagination method is not intuitive...

Post by allspiritseve »

Sindarin wrote: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.
Hmm.. so clicking next until you get to the fifth page displays different links than clicking on the number 5? That's really odd. Can you check and make sure the generated urls are the same? (For instance, the next page on page 4 compared to the link labeled '5'). If they are the same, then there shouldn't be any difference in rendering. If they aren't the same, then there's your problem ;)
Post Reply