May
27

Digg Style Pagination Class

Based on the modular version for the pagination, now I have created a version easier to implement, using classes for PHP version 4. The implementation is simple, is necessary to include the class with a require or an include. We defined basic properties for pagination such as an amount of elements to paginate, elements by page, page to which the element "page" will be sent, you need a CSS style and finally we generate the pagination to show it.

php:

include(ABSPATH.'/includes/pagination.class.php');
/*
Now you can work with de pagination class
*/

 
The class.
  • Version 0.4.1 (2008-04-11)
    1. Added getOutput() function.
  • Version 0.4 (2007-10-21)
    1. Added class next and prev to next and prev buttons.
    2. Bug fix
  • Version 0.3.3 (2007-10-05)
    1. Possibility to set 0 as the current page (no page selected). Back and Next buttons won't be shown.
  • Version 0.3.2 (2007-09-19)
    1. Changed round to ceil (line 122)
  • Version 0.3.1 (2007-08-23)
    1. Changed ceil to round (line 122)
  • Version 0.3 (2007-07-10)
    1. Added the URL friendly functionality.
  • Version 0.2 (2007-06-15)
    1. show an error and description if the function was called wrongly.
    2. Sends & instead of ? if the current file has vars in $_GET
    3. can change the name of the parameter ?page=
    4. there is no necessary to calculate in a manual way the pagination ($class->calculate()), is automatically when you show it the first time
    5. and others functionalities
  • Version 0.1 (2007-05-27)
    • First version

Dowload and unrar where you will work with the class. For the CSS, you can take one fromthis list.

If you forget a parameters to show or calculate the pagination. The class will show you a error to remember that. by example:

php:
$p=new pagination();
$p->show();

the results:

It is necessary to specify the number of pages ($class->items(1000))
It is necessary to specify the limit of items to show per page ($class->limit(10))

by default, the target of the pagination link is the same file where the paginations is showed. you can change it. using $class->target(file)

php:
$p = new pagination;
$p->items(1000);
$p->limit(10);
$p->target("?foo=var");
$p->show();
« Previous1234567...99100Next »

You can change the name of the parameter that brings the current page value. Using the $class->parameterName("p") you will see how the links changes the target to ?p=X instead of ?page=X

php:
$p = new pagination;
$p->items(1000);
$p->limit(10);
$p->currentPage(1);
$p->parameterName("p");
$p->show();
« Previous1234567...99100Next »

Defining the pagination on its basic way, specifying an amount of items, limits of items by page, the pointed file by the pagination and the number of the present page.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(2);
$p->show();

Defining in 1 the amount of adjacent pages to the present page.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(50);
$p->adjacents(1);
$p->show();

Defining in 3 the amount of adjacent pages to the present page.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(50);
$p->adjacents(3);
$p->show();

Defining in 4 the amount of adjacent pages to the present page.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(50);
$p->adjacents(4);
$p->show();

Activating the functionality to show the pages amount.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(6);
$p->showCounter(true);
$p->show();

Changing the text of the navigation tags and removing the icons.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(7);
$p->nextLabel('<strong>Siguiente</strong>');//changing next text
$p->prevLabel('<strong>Anterior</strong>');//changing previous text
$p->nextIcon('');//removing next icon
$p->prevIcon('');//removing previous icon
$p->show();

Removing the text of the navigation tags.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(8);
$p->nextLabel('');
$p->prevLabel('');
$p->show();
1234567891011...99100
(1000 Pages)

Changing the icons of the navigation tags

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("paginator.php");
$p->currentPage(9);
$p->nextIcon('&#9658;');
$p->prevIcon('&#9668;');
$p->show();
12...5678910111213...99100
(1000 Pages)

Removing the text of the navigation tags and changing the icons.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("#");
$p->currentPage(10);
$p->nextLabel('');//removing next text
$p->prevLabel('');//removing previous text
$p->nextIcon('&#9658;');//Changing the next icon
$p->prevIcon('&#9668;');//Changing the previous icon
$p->show();
12...67891011121314...99100
(1000 Pages)

Changing the div class (<div class="class">Pagination</div>) that surrounds the pagination (to manipulate many CSS styles)

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->target("#");
$p->currentPage(11);
$p->changeClass("jogger");
$p->show();
$p->changeClass("tres");
$p->show();
$p->currentPage(14);
$p->changeClass("meneame");
$p->show();
12...67891011121314...99100
(1000 Pages)
12...67891011121314...99100
(1000 Pages)
12...101112131415161718...99100
(1000 Pages)

To use the URL friendly, we need to execute the function $p->urlFriendly();. So we need to add to the $p->target("#"); the placeholder % where we need the page numbers. If we need to change the placeholder, we can do it send it the desired char as parameter to the function $p->urlFriendly('[...]');, and to the $p->target("#[...]");.

php:
$p = new pagination;
$p->Items(1000);
$p->limit(5);
$p->currentPage(14);

$p->urlFriendly();
$p->target("#page/%/");//#page/1/
$p->show();

$p->urlFriendly('[...]');
$p->target("#pagina/[...]/");//#pagina/1/
$p->show();

$p->urlFriendly(false);
$p->target("#");//#page=1
$p->show();
 
12...101112131415161718...99100
(1000 Pages)
12...101112131415161718...99100
(1000 Pages)
12...101112131415161718...99100
(1000 Pages)

51 Comments

Make A Comment
  • a gravatar Mis Algoritmos » Blog Archive » Some styles for your pagination Said:

    [...] Algoritmos El acordeón que todos necesitamos… « WP Digg Style Pagination Plugin Digg Style Pagination Class [...]

  • a gravatar Mis Algoritmos Said:

    How to create a digg and sabros.us style pagination with PHP

    [Demo] [WP-Digg Style Pagination Plugin] [Some Styles for your pagination]

    I made a few modifications to the sabros.us’s pagination script, inspired by the original created by strangerstudios.com, it needs a detail, turn it into a dynamic ...

  • a gravatar JL Said:

    Oye, en el momento que haces click sobre una de las paginas, te refresca la página agregando la línea "?page=n" (parámetro por GET) pero si yo intento utilizarla dentro de una página que ya utiliza el GET hace falta que ponga el ampersan (&) y agregue "page=n". De lo contrario no se podrá utilizar en páginas que ya tengan parámetros.

  • a gravatar Victor De la Rocha Said:

    @JL de hecho olvidé ese punto, le adicionaré ese detalle.

  • a gravatar Mis Algoritmos » Blog Archive » WP Digg Style Pagination Plugin Said:

    [...] If you wish to use the pagination in some of your systems programmed by own account you can use the Digg Style Pagination Class. A version easier to implement, using classes for PHP version 4. The implementation is simple, is [...]

  • a gravatar LM Said:

    Hi,
    A question.....
    How can we change the value of parameter that brings the current page value ?
    E.G :
    Instead of using, page=1, page=2.....page=n......
    I would like to use page=20, page=40,page=60,page=80....
    (the query is too complicated to change so this is the only way to display each items....)
    thanks for the answer

  • a gravatar Victor De la Rocha Said:

    @LM you can add $id *= 10; to the get_pagenum function:

    php:
    function get_pagenum_link($id){

                    $id *= 10;

                    if(strpos($this->target,'?')===false)
                                    return "$this->target?$this->parameterName=$id";
                            else
                                    return "$this->target&$this->parameterName=$id";
            }

  • a gravatar LM Said:

    Great !
    Thanks.
    I get another trouble.... :-)
    Well, it's working perfectly with the id incremented... but the pagination is still frozen on the page 1 even if you click on page 2,3,4,5,6......,n
    and of course the button Next and previous doesn't work.......
    Any idea ????

    Muchas gracias....

  • a gravatar Victor De la Rocha Said:

    @LM ok, I'm understand that. I will try to find a solution to your problem.

    The $_GET['page'] value for $class->currentPage() value, you need divide it by 10.

    php:
    if(isset($_GET['page'])){

                    $page = $_GET['page'] / 10;

                    $p->currentPage($page);
            }else
                    $p->currentPage(2);

  • a gravatar LM Said:

    Thanks....
    Where Do I need to modify in the pagination.class.php ?

  • a gravatar Victor De la Rocha Said:

    @LM You don't need to modify the pagination.class.php (only this). It only need to call the currentPage function with a value divided by 10.

    php:
    $p = new pagination;
    $p->items(1000);
    $p->limit(10);

    if(isset($_GET['page'])){
                    $page = $_GET['page'] / 10;
                    $p->currentPage($page);
            }else
                    $p->currentPage(1);

    $p->show();

  • a gravatar LM Said:

    Great !!
    it works goods....

    Thanks

  • a gravatar Mis Algoritmos » Blog Archive » How to paginate your SQL querys Said:

    [...] Mis Algoritmos Hi :) « Digg Style Pagination Class [...]

  • a gravatar Uri Said:

    Hey,
    I'm getting an error with the include of the pagination.class.php (it works only in the same folder)

    Error:
    "Fatal error: Cannot instantiate non-existent class: pagination in......"

    is there something i need to add in the pagination.class.php to solve that?

    tnx

  • a gravatar Victor De la Rocha Said:

    "Fatal error: Cannot instantiate non-existent class: pagination in......"

    You need to include the class.

  • a gravatar Aahz Said:

    I absolutely adore this Digg style pagination! Thank you, so much.

    However, tonight I installed Davide Pozza's Global Translator plugin and whenever I click over to an auto-translated page it wreaks havoc on my pagination. Any idea what I need to tweak to keep this from happening?

  • a gravatar chrispian Said:

    Line 122 reads:

    $lastpage = ceil($this->total_pages/$this->limit);

    Using ceil you often get an extra blank page at the end. I changed it to

    $lastpage = round($this->total_pages/$this->limit);

    And it works perfectly.

  • a gravatar Matun Said:

    Victor,

    I find your pagination class adorable! I am a newbie in php and it seems I can't get it working :~(
    I have a project for which I coded other kind of pagination (every single page number displays, no matter how many results there are!) which didn't suit my needs. Now I tried to fire up your navigation class but it doesn't work for me. I don't know how can I call it the right way because it always displays my first 10 rows from a database and nothing happens. Can you propose a solution maybe? How does your class know what rows it needs to displays when some other page from your pagination class is called?

  • a gravatar Kevin Said:

    I got the class working for general test purposes, but need help configuring it to friendly urls. Say the address is http://www.mysite.com/archive/site-news If they click the 2nd page it takes them to http://www.mysite.com/archive/site-news?page=2 but I want it to read http://www.mysite.com/archive/site-news/2 Can someone help with this?

  • a gravatar Kevin Said:

    Well I sort of got it to work, I used:

    $p->urlFriendly();
    $p->target('%');

    But this only works when the ending '/' is on the url. There has to be a way that works for both, right?

  • a gravatar Victor De la Rocha Said:

    :)

    php:
    $p->urlFriendly('[...]');
    $p->target("http://mysite.com/archive/site-news/[...]/");
  • a gravatar Kevin Said:

    Thank you very much, you have an awesome class and thanks for sharing and helping so many!!

  • a gravatar Wes Said:

    "Line 122 reads:

    $lastpage = ceil($this->total_pages/$this->limit);

    Using ceil you often get an extra blank page at the end. I changed it to

    $lastpage = round($this->total_pages/$this->limit);

    And it works perfectly."

    $lastpage = floor($this->total_pages/$this->limit);

    works better

  • a gravatar Di Said:

    Thanks verymatch for this paging class!

  • a gravatar Gawain Said:

    I've got the script mostly working but I'm getting hung up on one aspect. With $p->limit(10) queries that return 15 or more rows work perfectly, however on queries that return less than 15 rows the pagination is not displayed. I'd expect that to happen with less than 10 rows only.

    Am I doing something wrong, or do I need to adjust something within the class?

    Thanks!
    G

  • a gravatar Alex Said:

    The digg style solution here is great. Do you know how to incorporate this into Movable Type 4? I see that you have a Wordpress solution. It'd be great if this could also work in MT4. Thanks.

  • a gravatar Jason Coleman Said:

    This is great. Que excellente.

    This class implementation looks great, and the way to go for folks comfortable with classes.

    What plugin are you using to show code in your posts? I'm looking for a good one.

  • a gravatar Victor De la Rocha Said:

    Hola @Monsef, ¿Que version del plugin estas utilizando?Este problema fue solucionado en la última version de la classe (la 0.3.1). Intenta descargandola, si siguen los problemas, me dejas un mensaje :)

    @Alex: Maybe :)

    Hi Jason Coleman I use the Exec-PHP Plugin.

  • a gravatar WP Digg Style Pagination Plugin V 1.0 Said:

    [...] pagination class [...]

  • a gravatar Mike Grishaver Said:

    Nice class.

    I also found an issue when dealing with the last page. For example, if I have 21 items, with a limit of 10 items per page, I should get 3 pages.

    However, I am only getting 2 pages.

    Looking at the code, you have a function called round, this only rounds up if the number is above .5. So it'll round up correctly if you have 25 items, to show 3 pages. But if 21 items, only gives 2 pages.

    I changed the function from "round" to "ceil" to round up to the next integer and now it works.
    http://www.php.net/manual/en/function.ceil.php

  • a gravatar Victor De la Rocha Said:

    hmm @Mike Grishaver which is the solution? =S

  • a gravatar Glad you are not in charge of Wordpress Said:

    Hey Victor, I wasted over an hour already trying to figure out how to install your pagination plugin... Wordpress became the #1 blogging platform due to simplicity and ease of use, your plugin is overly complicated, even ridiculously so.

    Que quiere decir esto por ejemplo? (What does this mean?):

    "Based on the modular version for the pagination, now I have created a version easier to implement, using classes for PHP version 4. The implementation is simple, is necessary to include the class with a require or an include. We defined basic properties for pagination such as an amount of elements to paginate, elements by page, page to which the element "page" will be sent, you need a CSS style and finally we generate the pagination to show it."

    WHAT? Have you read the paragraph? I don't think YOU can make sense of it. Thank God that now it is "easier to implement", I don't want to imagine how complicated it was before.

    Víctor entonces y en resumen, si tu intención es compartir el plugin, hazlo simple de verdad, it's supposed to be a PLUG - IN, lo enchufas y listo. Este es una tesis camino a la maestría en filología digital.

    KISS principle comes to mind...

    Nos vemos!

  • a gravatar Monsef Said:

    Hola
    Hay mas error en tu classe , beuno 1 error no functiona correctamente con el FreindlyUrl , mustra paginas blancas he probado meter floor o ceil pero sgue iguale 3) cuando selectiona una pagina siempre se muesra la pagina 1 como selectionada asi que no coje la pagina selectionada si tenemos el urlRewriter activado , proba lo

    si tienes alguna solucion por fa ayudanos

    Gracias :D

  • a gravatar Victor De la Rocha Said:

    Uff, tienes razón y lo siento pero, no puedo darle gusto al mundo entero. Me contradije pero pues, no veo aún una manera mas simple de hacerlo.

    Además, mucho ojo! Esto no es un plugin ¡ES UNA CLASE en todo su esplendor! su uso es SIMPLE: lo incluyes en tu scripts y te apoyas de ella para trabajar con alguna de tus aplicaciones. Jamás mencioné que esto fuera un plugin y tampoco que estuviera basado en otro plugin.

    Si por alguna razón la paginación de consultas no fuese TU fuerte, intenté describir un ejemplo apoyándome de la clase =S

  • a gravatar bahodir Said:

    Any example how to use with MySQL queries?
    Thnx.

  • a gravatar Victor De la Rocha Said:

    yes @bahodir, you can read this

  • a gravatar Como hacer un Buscador con Ajax Said:

    [...] Aquí [...]

  • a gravatar Paginación para Wordpress y tus scripts en PHP Said:

    [...] Digg style pagination class [...]

  • a gravatar marko Said:

    Thank you very much for your great script, works like a charm and easy to use. 5/5 :)

  • a gravatar Paul Said:

    need help
    Hi...I am using your pagination class and I need your help. Pagination is working ok when I open the page first time but when I click on page links like 1,2,3,4 ....it says page can not be displayed. I am passing variables with data between pages using URL. When I put my cursor on these links 1,2,3,4,...etc, it doesn't show variables are attached to the url string. like in status bar it shows "http://localhost/filename.php?page=2". If I use another pagination it shows "http://localhost/filename.php?page=2Field=test1find=test2...". Here field and find are variables and test1 and test2 are values given to the variables.Do I need to change some code in the pagination class? Thanks for your nice pagination.

  • a gravatar Paul Said:

    sorry another pagination is like "http://localhost/filename.php?page=2&Field=test1&find=test2".

    Thanks

  • a gravatar Paul Said:

    Hi

    I am able to pass the parameters with url and pagination is working fine but it doesn't change the records. It just stays at page one and numbers at pagination get changed. Any help?

    Thanks

  • a gravatar Alex Said:

    Hello...I am using limit 10 and your pagination is working fine but its showing results starting from 11(i.e. pagination link 1 starts from 11 to 20, pagination link 2 shows 21 to 30 and so on) and the last page is blank. I tried Floor or round at line 122 but it doesn't help me. I need your help.

    thanks

  • a gravatar Luiz Said:

    Same to me.

    I have 25 limit. And I have one blank page at last, chage to "round" or "floor" I dont get last results.

  • a gravatar WaFi Said:

    I need Example very simple ... Can you do That ?

    (use select of table name of database)

    Plz ....

    I watting You ..

  • a gravatar Andre Medeiros Said:

    On line 148 of your class, it reads:

    -----------------------------8page adjacents * 2)){
    -----------------------------8<-----------------------------

    I got a result, with 20 pages, on the 4th page, with something like:

    < 1 2 ... 3 4 5 (etc)

    In order to solve this, change the "if" statement to:
    -----------------------------8page adjacents * 2)){
    -----------------------------8<-----------------------------

    Other than that, excelent job!

  • a gravatar Tybek Said:

    Hi, Thankyou for the class.

    I made a modification to one function in your class in order for it to be xhtml compliant since I had problems getting it validated. Basically I added htmlspecialchars(). It now validates properly.

    New function follows:

    function get_pagenum_link($id){
    if(strpos($this->target,'?')===false)
    if($this->urlF)
    return str_replace($this->urlF,$id,$this->target);
    else
    return "$this->target?$this->parameterName=$id";
    else
    return htmlspecialchars("$this->target&$this->parameterName=$id");
    }

  • a gravatar Deep Said:

    Did I tell you, your code simply rocks.. I was waiting for this kind of pagination class and finally found it :)

  • a gravatar turboliux blog » Blog Archive » Pamoka. Diegiam Wordpress puslapių numeravimą Said:

    [...] įvairiausių šio kodo variacijų, tačiau siūlau rinktis paprastesnį. Galimi du instaliavimo variantai, arba jums teks šitą [...]

  • a gravatar Renzoster Said:

    Thanks for this script

  • a gravatar gabriel Said:

    muy bueno lo tuyo!

Comments RSS Feed   TrackBack URL

Leave a comment

top