CrankBerry Blog Title
2009


(5) Comments

Simple dynamic PHP sliding pagination

The other day I was working on a website drawing out a content well over 400 rows of data. Displaying the data all on one page is a lot of stress for the user so I made a quick pagination script. By displaying 20 results per page would end up with 20 separate pages. Imaging laying out 1 to 20 across your website (ugly eh?) well it gets worst. Imagine this database doubles! Yes! We're now laying 1 to 40 across our website. So I made this simple dynamic slider in which it only displays a certain amount of pages to choose from and as you travel further it will list more pages.

Who is this script for?gd

This script does not allow the option of displaying all of the pages at once; thus, if you have a ginormous database that the user needs to quickly browse through, this is not for you. If you only have 3 or 4 pages of static content, this is not for you. If you need a real-time sliding pagination script, this is not for you. If you like PEPSI, this is not for you.

Example

If at this point you still don't understand what I'm talking about then I have failed what I came here to do. Better yet, let me explain it to you in 3000 words.

So here you see you're on page 1 of 9 but there's like 20+ pages to be shown.

Dynamic PHP Pagination 1

As you move closer to the end of the list (so now you're on page 8) you get to see further down the list

Dynamic PHP Pagination 2

With your selection always being in the center, except when you're near the beginning or end of the full list.

Dynamic PHP Pagination 3

Enought Chat, Give me the Script!

The first part of the script goes where you want the pagination to appear.

//Simple Dynamic PHP Sliding Pagination by Jerry Low @crankberryblog.com
//Find other useful scripts at the Crankberry Blog

//Here Are the Variables you need to set to use the display pages function
//Start – Add in your variables below

//Page total is the total number of items that you have for the user to go
//through. You can define this yourself if you have a lot of static content
//of feed your database row count into here.

$page_total = 400;

//Page Show is the number of items you want to show per page. You should add
//this number in yourself. Depending on how you display your results. The default
//is set to show 20 items per page.

$page_show = 20;

//Page Pages basically how many page options you want to show to the user per
//time. If you have 20 pages you can choose to show 5 pages to select per time.
//REMINDER: the number you select, if its even it will go one up. So if you choose
//8 it will actually show 9 options, this is because the dynamic scroller will
//always be in the center and even numbers don't really have a center.

$page_pages = 5;

//Page align just sets where the pagingation should sit in your site.
//Select from 'left', 'center' or 'right

$page_align = 'left';

//OPTIONAL – Page extention is any other variables you are passing between pages
//Just add it on here eg. sort=whatever&order=whatever

$page_extention = "";

//End – Adding Variables

//The script below grabs the current page they're on
if (isset($_GET['p']) && is_numeric($_GET['p'])) $current_pageNum = $_GET['p'];
else $current_pageNum = 1;

//Forming the Extention
if (isset($page_extention) && $page_extention!="") $page_extention = '&' . $page_extention;

//Initiating the Pagination… OMG
$page_offset = display_pages($current_pageNum, $page_total, $page_show, $page_pages, $current_page, $page_align, $page_extention);

The end will return a $page_offset which is to be used if you have a database. If you are using static content then don't worry about it.

The next part is the function, this can go in the same page or you can stick it into your class. Note: If you stick it in your class you have to call the object for the function.

function display_pages($current_pageNum, $page_total, $page_show = 20, $page_pages, $current_page, $page_align, $page_extention = NULL) {
        //Don't Show pagination if there isn't more than 1 page
        if ($page_total>$page_show) {
                //If Page number exceeds then just show page 1
                if ($current_pageNum>$page_total || $current_pageNum<1) $current_pageNum = 1;
               
                //Getting Page Alignment
                switch ($page_align) {
                        case 'left':
                                $div_align = 'left';
                                break;
                        case 'center':
                                $div_align = 'center';
                                break;
                        case 'right':
                                $div_align = 'right';
                                break;
                        default:
                                $div_align = 'left';
                                break;
                }
               
                //Start Div
                echo '<div align="'.$div_align.'">Pages: ';
               
                //Setting Var
                $page_prev = $current_pageNum-1;
                $page_next = $current_pageNum+1;
               
                //Displaying Prev Page
                if ($current_pageNum!=1) echo '<a href="'.$current_page.'?p='.$page_prev.$page_extention.'">&laquo; </a>';
               
                //Making The Pages
                $page_count = ceil($page_total/$page_show);
               
                //For Page Pages
                $page_pages;
                $page_padding = floor($page_pages/2);
               
                //If There Are More Pages
                if ($page_count>$page_pages) {
                        //Measuring Page Start
                        if ($current_pageNum>=($page_padding+1)) {
                                //If They're Near The End
                                if ($current_pageNum>($page_count-$page_padding)) {
                                        //Where To Start Page
                                        $page_start = $page_count-($page_padding*2);
                                        $page_end = $page_count;
                                } else {
                                        $page_start = $current_pageNum-$page_padding;
                                        $page_end = $current_pageNum+$page_padding;
                                }
                        } else {
                                $page_start = 1;
                                $page_end = ($page_padding*2)+1;
                        }
                } else {
                        //The Vars
                        $page_start = 1;
                        $page_end = $page_count;
                }
               
                for($t=$page_start; $t<=$page_end; $t++) {
                        //Displaying Pages
                        if ($current_pageNum==$t) echo '<strong>';
                        echo '<a href="'.$current_page.'?p='.$t.$page_extention.'">'.$t.' </a>';
                        if ($current_pageNum==$t) echo '</strong>';
                }
               
                //Displaying Next Page
                if ($current_pageNum!=$page_count) echo '<a href="'.$current_page.'?p='.$page_next.$page_extention.'">&raquo; </a>';
               
                //Displaying Last Page
                echo '<a href="'.$current_page.'?p='.$page_count.$page_extention.'">Last </a>
'
;
               
                //Page Number Results
                $page_display_low = (($current_pageNum-1)*$page_show)+1;
                $page_display_high = ($page_display_low+$page_show)-1;
                if ($page_display_high>$page_total) $page_display_high = $page_total;
                echo '<font style="font-size: 9px;">(Displaying: '.$page_display_low.'-'.$page_display_high.' of '.$page_total.')</font>';
               
                //End Div
                echo '</div>';

                //Offset
                $page_final = ($current_pageNum-1)*$page_show;
               
                return $page_final;
        } else {
                return 0;       
        }
}

See It In Action

So here is an example of me calling up some data and running it through the script.

//Some SQL Connection Made
$get_HugeTable = 'SELECT * FROM huge_table';
$get_rows = mysql_num_rows( mysql_query( $get_HugeTable ) );

//Filling out the Variables
$page_total = $get_rows;
$page_show = 20;
$pages_pages = 10;
$page_align = 'right';

//Call The Function
$page_offset = display_pages($current_pageNum, $page_total, $page_show, $page_pages, $current_page, $page_align, $page_extention); //No Classes
$page_offset = $my_classes->display_pages($current_pageNum, $page_total, $page_show, $page_pages, $current_page, $page_align, $page_extention); //With Classes

//Displaying the Actual Data
$display_HugeTable = 'SELECT * FROM huge_table ORDER BY huge_name ASC LIMIT $page_offset, $page_show';
$display_HugeTable_query = mysql_query($display_HugeTable);

while ($HugeTable = mysql_fetch_array($display_HugeTable_query)) {
        echo $HugeTable['huge_name'] . '
'
;
}

TL
This entry was posted on Thursday, September 24th, 2009 at 9:45 pm and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
BL

5 Responses to “Simple dynamic PHP sliding pagination”

  1. Jerry Jerry Low says:

    Olive,

    This script does require MySQL. I suppose theoretically if you weren't using some type of database then your content should be static. In that case you would need to hard code your pages in rather than using this script. Now you could also be utilizing some other database system which is fine too because you can still feed the numbers into the function.

    This code resides in the body part of your page and placed exactly where you want to see the pages be display, now that being said you'll still have to place this code above your outputted entries.

    You'll need the function because the "See it in action" section actually pulls it from the function. The "See it in Action" is just for a reference as of how to activate the code.

    -Jerry

  2. Olive Olive says:

    Hi Jerry,

    I would like to ask few questions about this code, coz I don't know much about this :

    1. Does this code need mysql? (I would prefer not to use mysql)
    2. Which part should I have to put this code? (head or body section)
    3. Can I just copy the short code in "See It In Action" part or I have to use the whole code in "First Part" and "Function Part"?

    Please kindly help. Thanks

  3. deallymam deallymam says:

    Hello Kickback klooper as a help to my english jer, buti discriminative nice re application .

  4. Marcus Marcus says:

    Wow, pretty advanced stuff. The end result is real nice, keep up the good work man.

Leave a Reply

Spam protection by WP Captcha-Free