1

Topic: Another Pagination helper

<?php
/**
 * Simple Pager helper based on the Kohana pagination helper.
 *
 * @author    Maslakov Alexander <jmas.ukraine@gmail.com>
 * @author    Kohana helper
 * @license    [url]http://kohanaphp.com/license.html[/url]
 */

class Pager
{
    // Link templates
    protected $link_tag = '<a href=":link">:name</a>';
    protected $current_tag = '<strong class="current">:name</strong>';
    protected $prev_tag = '<a href=":link" class="prev">&lsaquo;</a>';
    protected $prev_text_tag = '<span class="prev">&lsaquo;</span>';
    protected $next_tag = '<a href=":link" class="next">&rsaquo;</a>';
    protected $next_text_tag = '<span class="next">&rsaquo;</span>';
    protected $first_tag = '<a href=":link" class="first">&laquo;</a>';
    protected $last_tag = '<a href=":link" class="last">&raquo;</a>';
    protected $extended_pageof = 'Page :current_page of :total_pages';
    protected $extended_itemsof = 'Showing items :current_first_item &mdash; :current_last_item of :total_items';
    
    // Config values
    protected $base_url = '?';
    protected $items_per_page = 15;
    protected $total_items = 0;
    protected $query_string = 'page';
    protected $style = 'classic';

    // Autogenerated values
    protected $url;
    protected $current_page;
    protected $total_pages;
    protected $current_first_item;
    protected $current_last_item;
    protected $first_page;
    protected $last_page;
    protected $previous_page;
    protected $next_page;    
    protected $sql_offset;
    protected $sql_limit;
    
    /**
     * Constructs a new Pager object.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function __construct($config = array())
    {
        $this->initialize($config);
    }
    
    /**
     * Sets config values.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function initialize($config = array())
    {
        foreach ($config as $key => $value)
        {
            if (property_exists($this, $key))
            {
                $this->$key = $value;
            }
        }
        
        $this->url = $this->base_url;
        
        // Extract current page
        $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1;

        // Insert {page} placeholder
        $_GET[$this->query_string] = '{page}';

        // Create full URL
        $this->url = $this->url . str_replace('%7Bpage%7D', '{page}', http_build_query($_GET));

        // Reset page number
        $_GET[$this->query_string] = $this->current_page;
        
        $this->total_items        = (int) max(0, $this->total_items);
        $this->items_per_page     = (int) max(1, $this->items_per_page);
        $this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
        $this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
        $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
        $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
        
        $this->first_page         = ($this->current_page === 1) ? FALSE : 1;
        $this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
        $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
        $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
        
        $this->sql_offset         = (int) ($this->current_page - 1) * $this->items_per_page;
        $this->sql_limit          = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset);
    }
    
    /**
     * Generates the HTML for the chosen pagination style.
     *
     * @return  string  pagination html
     */
    public function render()
    {
        $out = '<p class="pager">'."\n";
        
        switch($this->style)
        {
            // ‹ First  < 1 2 3 >  Last ›
            case 'classic':
            default:
    
                if($this->first_page){
                    $out .= str_replace(':link', str_replace('{page}', 1, $this->url), $this->first_tag)."\n";
                }
                
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
            
                for ($i = 1; $i <= $this->total_pages; $i++)
                {
                    if ($i == $this->current_page) {
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else {
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                if($this->last_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->last_page, $this->url), $this->last_tag)."\n";
                }
                
                break;
            
            // « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next »
            case 'digg':
            
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
                
                if ($this->total_pages < 13) /* « Previous  1 2 3 4 5 6 7 8 9 10 11 12  Next » */
                {
                    for ($i = 1; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                elseif ($this->current_page < 9) /* « Previous  1 2 3 4 5 6 7 8 9 10 … 25 26  Next » */
                {
                    for ($i = 1; $i <= 10; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                elseif ($this->current_page > $this->total_pages - 8) /* « Previous  1 2 … 17 18 19 20 21 22 23 24 25 26  Next » */
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->total_pages - 9; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                else /* « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next » */
                { 

                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->current_page - 5; $i <= $this->current_page + 5; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                break;
                
            // « Previous | Page 2 of 11 | Showing items 6-10 of 52 | Next »
            case 'extended':
                
                if ($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }else{
                    $out .= $this->prev_text_tag;
                }

                $out .= ' <span class="vertical">|</span> ' . str_replace(array(':current_page', ':total_pages'), array($this->current_page, $this->total_pages), $this->extended_pageof).
                        ' <span class="vertical">|</span> ' . str_replace(array(':current_first_item', ':current_last_item', ':total_items'), array($this->current_first_item, $this->current_last_item, $this->total_items), $this->extended_itemsof).
                        ' <span class="vertical">|</span> '."\n";
        
                if ($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }else{
                    $out .= $this->next_text_tag;
                }
            
                break;
                
            // Pages: 1 … 4 5 6 7 8 … 15
            case 'punbb':
            
                if ($this->current_page > 3)
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    if ($this->current_page != 4) $out .= '&hellip;'."\n";
                }

                for ($i = $this->current_page - 2, $stop = $this->current_page + 3; $i < $stop; ++$i)
                {
                    if ($i < 1 OR $i > $this->total_pages) continue;

                    if ($this->current_page == $i){
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else{
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }

                }

                if ($this->current_page <= $this->total_pages - 3)
                {
                    if ($this->current_page != $this->total_pages - 3) $out .= '&hellip;'."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                break;
        }
        
        return $out . '</p>'."\n";
    }
    
    /**
     * Magically converts Pagination object to string.
     *
     * @return  string  pagination html
     */
    public function __toString()
    {
        return $this->render();
    }
    
    /**
     * Magically gets a pagination variable.
     *
     * @param   string  variable key
     * @return  mixed   variable value if the key is found
     * @return  void    if the key is not found
     */
    public function __get($key)
    {
        if (isset($this->$key))
            return $this->$key;
    }

    /**
     * Adds a secondary interface for accessing properties, e.g. $pager->total_pages().
     * Note that $pagination->total_pages is the recommended way to access properties.
     *
     * @param   string  function name
     * @return  string
     */
    public function __call($func, $args = NULL)
    {
        return $this->__get($func);
    }
    
} // End Pager Class

?>

Plese, test it for debuggin...

For use it, put next code in site Layout:

use_helper('Pager');

echo Pager(array(
     'total_items' => $this->childrenCount()
));

Last edited by jMas (2010-01-26 14:01)

Thumbs up

2

Re: Another Pagination helper

jMas wrote:

Plese, test it for debuggin...

For use it, put next code in site Layout: ...

Hi jMas - thanks for this! I would like to test it, but I have a question: is there any way to set the number of items displayed on each page? Example: if I want to display only 4 items on each page (or 3, or 10, etc.) ... how do I do that?

Thanks!

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

3

Re: Another Pagination helper

David wrote:

...but I have a question: is there any way to set the number of items displayed on each page? Example: if I want to display only 4 items on each page (or 3, or 10, etc.) ... how do I do that?
Thanks!

David, I think its self-explanatory,
you have to set it in the //config variables

protected $items_per_page = 15

that should do.

4

Re: Another Pagination helper

Updated code:

<?php
/**
 * Simple Pager helper based on the Kohana pagination helper.
 *
 * @author    Maslakov Alexander <jmas.ukraine@gmail.com>
 * @author    Kohana helper
 * @license    [url]http://kohanaphp.com/license.html[/url]
 */

class Pager
{
    // Link templates
    protected $link_tag = '<a href=":link">:name</a>';
    protected $current_tag = '<strong class="current">:name</strong>';
    protected $prev_tag = '<a href=":link" class="prev">&lsaquo;</a>';
    protected $prev_text_tag = '<span class="prev">&lsaquo;</span>';
    protected $next_tag = '<a href=":link" class="next">&rsaquo;</a>';
    protected $next_text_tag = '<span class="next">&rsaquo;</span>';
    protected $first_tag = '<a href=":link" class="first">&laquo;</a>';
    protected $last_tag = '<a href=":link" class="last">&raquo;</a>';
    protected $extended_pageof = 'Page :current_page of :total_pages';
    protected $extended_itemsof = 'Showing items :current_first_item &mdash; :current_last_item of :total_items';
    
    // Config values
    protected $base_url = '?';
    protected $items_per_page = 15;
    protected $total_items = 0;
    protected $query_string = 'page';
    protected $style = 'classic';

    // Autogenerated values
    protected $url;
    protected $current_page;
    protected $total_pages;
    protected $current_first_item;
    protected $current_last_item;
    protected $first_page;
    protected $last_page;
    protected $previous_page;
    protected $next_page;    
    protected $sql_offset;
    protected $sql_limit;
    
    /**
     * Constructs a new Pager object.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function __construct($config = array())
    {
        $this->initialize($config);
    }
    
    /**
     * Sets config values.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function initialize($config = array())
    {
        foreach ($config as $key => $value)
        {
            if (property_exists($this, $key))
            {
                $this->$key = $value;
            }
        }
        
        $this->url = $this->base_url;
        
        // Extract current page
        $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1;

        // Insert {page} placeholder
        $_GET[$this->query_string] = '{page}';

        // Create full URL
        $this->url = $this->url . str_replace('%7Bpage%7D', '{page}', http_build_query($_GET));

        // Reset page number
        $_GET[$this->query_string] = $this->current_page;
        
        $this->total_items        = (int) max(0, $this->total_items);
        $this->items_per_page     = (int) max(1, $this->items_per_page);
        $this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
        $this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
        $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
        $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
        
        $this->first_page         = ($this->current_page === 1) ? FALSE : 1;
        $this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
        $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
        $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
        
        $this->sql_offset         = (int) ($this->current_page - 1) * $this->items_per_page;
        $this->sql_limit          = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset);
    }
    
    /**
     * Generates the HTML for the chosen pagination style.
     *
     * @return  string  pagination html
     */
    public function render()
    {
        $out = '<p class="pager">'."\n";
        
        switch($this->style)
        {
            // ‹ First  < 1 2 3 >  Last ›
            case 'classic':
            default:
    
                if($this->first_page){
                    $out .= str_replace(':link', str_replace('{page}', 1, $this->url), $this->first_tag)."\n";
                }
                
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
            
                for ($i = 1; $i <= $this->total_pages; $i++)
                {
                    if ($i == $this->current_page) {
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else {
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                if($this->last_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->last_page, $this->url), $this->last_tag)."\n";
                }
                
                break;
            
            // « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next »
            case 'digg':
            
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
                
                if ($this->total_pages < 13) /* « Previous  1 2 3 4 5 6 7 8 9 10 11 12  Next » */
                {
                    for ($i = 1; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                elseif ($this->current_page < 9) /* « Previous  1 2 3 4 5 6 7 8 9 10 … 25 26  Next » */
                {
                    for ($i = 1; $i <= 10; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                elseif ($this->current_page > $this->total_pages - 8) /* « Previous  1 2 … 17 18 19 20 21 22 23 24 25 26  Next » */
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->total_pages - 9; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                else /* « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next » */
                { 

                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->current_page - 5; $i <= $this->current_page + 5; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                break;
                
            // « Previous | Page 2 of 11 | Showing items 6-10 of 52 | Next »
            case 'extended':
                
                if ($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }else{
                    $out .= $this->prev_text_tag;
                }

                $out .= ' <span class="vertical">|</span> ' . str_replace(array(':current_page', ':total_pages'), array($this->current_page, $this->total_pages), $this->extended_pageof).
                        ' <span class="vertical">|</span> ' . str_replace(array(':current_first_item', ':current_last_item', ':total_items'), array($this->current_first_item, $this->current_last_item, $this->total_items), $this->extended_itemsof).
                        ' <span class="vertical">|</span> '."\n";
        
                if ($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }else{
                    $out .= $this->next_text_tag;
                }
            
                break;
                
            // Pages: 1 … 4 5 6 7 8 … 15
            case 'punbb':
            
                if ($this->current_page > 3)
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    if ($this->current_page != 4) $out .= '&hellip;'."\n";
                }

                for ($i = $this->current_page - 2, $stop = $this->current_page + 3; $i < $stop; ++$i)
                {
                    if ($i < 1 OR $i > $this->total_pages) continue;

                    if ($this->current_page == $i){
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else{
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }

                }

                if ($this->current_page <= $this->total_pages - 3)
                {
                    if ($this->current_page != $this->total_pages - 3) $out .= '&hellip;'."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                break;
        }
        
        return $out . '</p>'."\n";
    }
    
    /**
     * Magically converts Pagination object to string.
     *
     * @return  string  pagination html
     */
    public function __toString()
    {
        return $this->render();
    }
    
    /**
     * Magically gets a pagination variable.
     *
     * @param   string  variable key
     * @return  mixed   variable value if the key is found
     * @return  void    if the key is not found
     */
    public function __get($key)
    {
        if (isset($this->$key))
            return $this->$key;
    }

    /**
     * Adds a secondary interface for accessing properties, e.g. $pager->total_pages().
     * Note that $pagination->total_pages is the recommended way to access properties.
     *
     * @param   string  function name
     * @return  string
     */
    public function __call($func, $args = NULL)
    {
        return $this->__get($func);
    }
    
} // End Pager Class

?>

Last edited by jMas (2010-01-26 00:09)

Thumbs up

5

Re: Another Pagination helper

Thanks axjezzy & jMas! I have been missing these today -- for some reason punBB didn't signal that there were new posts here!

Looking forward to trying this out tomorrow!

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

6

Re: Another Pagination helper

Hmmm... Not working so well for me, jMas. I am more stupid than you think! wink

You can see what's happening on the Articles page of myTestWolf site. Ugly. I have it set to give me 3 items per page. But it always lists all of them, and the last ">>" link is badly formed. Here's what I have in the Articles page itself:

<?php use_helper('Pager'); ?>

    // here is the foreach loop for calling the article page links

<?php $totalPages = $this->childrenCount();
  echo new Pager(array(
    'total_items' => $totalPages,      // Total items count
    'items_per_page' => 3,    // Number of items on one page
    'style' => 'classic'       // Style of pagination tool
)); ?>

I found that if you don't give it the total, it won't work at all (which seems a bit odd). But that's why I have used the "dynamic" childrenCount() function here.

Any ideas?

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

7

Re: Another Pagination helper

Try this code:

<?php 

use_helper('Pager'); 

$pager = new Pager(array('total_items' => $this->childrenCount())); 

?>

<ul>
<?php 

foreach ($this->children(array('limit' => $pager->items_per_page, 'offset' => $pager->sql_offset)) as $item){
     echo "<li>{$item->link()}</li>";
} 

?>
</ul>

<?php echo $pager ?>

Last edited by jMas (2010-01-25 14:30)

Thumbs up

8

Re: Another Pagination helper

That works, jMas! Good job!

Only one thing at the moment (I could probably work it out if I had a moment to look at it!) -- in the "classic" style, the "last page" link (») is not correctly formed. It points just to "3" instead of "articles?page=3" (you can see it on my articles page). Any suggestions?

Meanwhile, looks like a very versatile and helpful Helper! Thanks!

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

9

Re: Another Pagination helper

Why does mine break the whole page content in the very first line when I include the helper.

use_helper('Pager');

it's very odd -_-'

10

Re: Another Pagination helper

David, thanks, yes it is small bug on line 138.

Updated code:

<?php
/**
 * Simple Pager helper based on the Kohana pagination helper.
 *
 * @author    Maslakov Alexander <jmas.ukraine@gmail.com>
 * @author    Kohana helper
 * @license    http://kohanaphp.com/license.html
 */

class Pager
{
    // Link templates
    protected $link_tag = '<a href=":link">:name</a>';
    protected $current_tag = '<strong class="current">:name</strong>';
    protected $prev_tag = '<a href=":link" class="prev">&lsaquo;</a>';
    protected $prev_text_tag = '<span class="prev">&lsaquo;</span>';
    protected $next_tag = '<a href=":link" class="next">&rsaquo;</a>';
    protected $next_text_tag = '<span class="next">&rsaquo;</span>';
    protected $first_tag = '<a href=":link" class="first">&laquo;</a>';
    protected $last_tag = '<a href=":link" class="last">&raquo;</a>';
    protected $extended_pageof = 'Page :current_page of :total_pages';
    protected $extended_itemsof = 'Showing items :current_first_item &mdash; :current_last_item of :total_items';
    
    // Config values
    protected $base_url = '?';
    protected $items_per_page = 15;
    protected $total_items = 0;
    protected $query_string = 'page';
    protected $style = 'classic';

    // Autogenerated values
    protected $url;
    protected $current_page;
    protected $total_pages;
    protected $current_first_item;
    protected $current_last_item;
    protected $first_page;
    protected $last_page;
    protected $previous_page;
    protected $next_page;    
    protected $sql_offset;
    protected $sql_limit;
    
    /**
     * Constructs a new Pager object.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function __construct($config = array())
    {
        $this->initialize($config);
    }
    
    /**
     * Sets config values.
     *
     * @param   array  configuration settings
     * @return  void
     */
    function initialize($config = array())
    {
        foreach ($config as $key => $value)
        {
            if (property_exists($this, $key))
            {
                $this->$key = $value;
            }
        }
        
        $this->url = $this->base_url;
        
        // Extract current page
        $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1;

        // Insert {page} placeholder
        $_GET[$this->query_string] = '{page}';

        // Create full URL
        $this->url = $this->url . str_replace('%7Bpage%7D', '{page}', http_build_query($_GET));

        // Reset page number
        $_GET[$this->query_string] = $this->current_page;
        
        $this->total_items        = (int) max(0, $this->total_items);
        $this->items_per_page     = (int) max(1, $this->items_per_page);
        $this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
        $this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
        $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
        $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
        
        $this->first_page         = ($this->current_page === 1) ? FALSE : 1;
        $this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
        $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
        $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
        
        $this->sql_offset         = (int) ($this->current_page - 1) * $this->items_per_page;
        $this->sql_limit          = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset);
    }
    
    /**
     * Generates the HTML for the chosen pagination style.
     *
     * @return  string  pagination html
     */
    public function render()
    {
        $out = '<p class="pager">'."\n";
        
        switch($this->style)
        {
            // ‹ First  < 1 2 3 >  Last ›
            case 'classic':
            default:
    
                if($this->first_page){
                    $out .= str_replace(':link', str_replace('{page}', 1, $this->url), $this->first_tag)."\n";
                }
                
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
            
                for ($i = 1; $i <= $this->total_pages; $i++)
                {
                    if ($i == $this->current_page) {
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else {
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                if($this->last_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->last_page, $this->url), $this->last_tag)."\n";
                }
                
                break;
            
            // « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next »
            case 'digg':
            
                if($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }
                
                if ($this->total_pages < 13) /* « Previous  1 2 3 4 5 6 7 8 9 10 11 12  Next » */
                {
                    for ($i = 1; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                elseif ($this->current_page < 9) /* « Previous  1 2 3 4 5 6 7 8 9 10 … 25 26  Next » */
                {
                    for ($i = 1; $i <= 10; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                elseif ($this->current_page > $this->total_pages - 8) /* « Previous  1 2 … 17 18 19 20 21 22 23 24 25 26  Next » */
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->total_pages - 9; $i <= $this->total_pages; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }
                }
                else /* « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next » */
                { 

                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 2, $this->url), 2), $this->link_tag)."\n";
                    
                    $out .= '&hellip;'."\n";

                    for ($i = $this->current_page - 5; $i <= $this->current_page + 5; $i++){
                        if ($i == $this->current_page){
                            $out .= str_replace(':name', $i, $this->current_tag)."\n";
                        }else{
                            $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                        }
                    }

                    $out .= '&hellip;'."\n";
                    
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages - 1, $this->url), $this->total_pages - 1), $this->link_tag)."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                if($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }
                
                break;
                
            // « Previous | Page 2 of 11 | Showing items 6-10 of 52 | Next »
            case 'extended':
                
                if ($this->previous_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->previous_page, $this->url), $this->prev_tag)."\n";
                }else{
                    $out .= $this->prev_text_tag;
                }

                $out .= ' <span class="vertical">|</span> ' . str_replace(array(':current_page', ':total_pages'), array($this->current_page, $this->total_pages), $this->extended_pageof).
                        ' <span class="vertical">|</span> ' . str_replace(array(':current_first_item', ':current_last_item', ':total_items'), array($this->current_first_item, $this->current_last_item, $this->total_items), $this->extended_itemsof).
                        ' <span class="vertical">|</span> '."\n";
        
                if ($this->next_page){
                    $out .= str_replace(':link', str_replace('{page}', $this->next_page, $this->url), $this->next_tag)."\n";
                }else{
                    $out .= $this->next_text_tag;
                }
            
                break;
                
            // Pages: 1 … 4 5 6 7 8 … 15
            case 'punbb':
            
                if ($this->current_page > 3)
                {
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', 1, $this->url), 1), $this->link_tag)."\n";
                    if ($this->current_page != 4) $out .= '&hellip;'."\n";
                }

                for ($i = $this->current_page - 2, $stop = $this->current_page + 3; $i < $stop; ++$i)
                {
                    if ($i < 1 OR $i > $this->total_pages) continue;

                    if ($this->current_page == $i){
                        $out .= str_replace(':name', $i, $this->current_tag)."\n";
                    }else{
                        $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $i, $this->url), $i), $this->link_tag)."\n";
                    }

                }

                if ($this->current_page <= $this->total_pages - 3)
                {
                    if ($this->current_page != $this->total_pages - 3) $out .= '&hellip;'."\n";
                    $out .= str_replace(array(':link', ':name'), array(str_replace('{page}', $this->total_pages, $this->url), $this->total_pages), $this->link_tag)."\n";
                }
                
                break;
        }
        
        return $out . '</p>'."\n";
    }
    
    /**
     * Magically converts Pagination object to string.
     *
     * @return  string  pagination html
     */
    public function __toString()
    {
        return $this->render();
    }
    
    /**
     * Magically gets a pagination variable.
     *
     * @param   string  variable key
     * @return  mixed   variable value if the key is found
     * @return  void    if the key is not found
     */
    public function __get($key)
    {
        if (isset($this->$key))
            return $this->$key;
    }

    /**
     * Adds a secondary interface for accessing properties, e.g. $pager->total_pages().
     * Note that $pagination->total_pages is the recommended way to access properties.
     *
     * @param   string  function name
     * @return  string
     */
    public function __call($func, $args = NULL)
    {
        return $this->__get($func);
    }
    
} // End Pager Class

?>

axjezzy, its very strange. Maybe in Pager.php after "?>" you have empty lines?

Last edited by jMas (2010-01-25 21:47)

Thumbs up

11

Re: Another Pagination helper

axjezzy wrote:

Why does mine break the whole page content in the very first line when I include the helper.

use_helper('Pager');

Did you create a "Pager.php" file in the wolf/helpers folder, and copy the contents of the first code block in post #4 into it? I assume you did, but might as well start with the obvious! wink

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

12

Re: Another Pagination helper

@jMas - nice fix - perfect! Thanks!

@axjezzy - while we're thinking ... it also MUST be "Pager.php" with a capital "P", not "pager.php" with little "p".

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

13

Re: Another Pagination helper

Nice work jMas and others. Just found this and it seems to work pretty well. Thanks for your work.

Thumbs up

14

Re: Another Pagination helper

By the way, I have made wiki pages for both of the pagination helpers (default, and this one from jMas). If you're in this thread, you might be interested in reading the documentation for Pager.

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

15

Re: Another Pagination helper

THANKS! That looks great. Very helpful.

Thumbs up

16

Re: Another Pagination helper

How can I suggest a little change in the code and adding a little rewrite magic to enable nice url ??
I mean having this kind of URL /page/xxx instead of default one ?page=xxx

Is there a github or this post is fine ??

17

Re: Another Pagination helper

I think this post i fine as I haven't found github account. There is also this article in the wiki, so you can add your modded helper or connect it with your github acc.

Tutorials at Project 79 | Wolf CMS Docs

18

Re: Another Pagination helper

Didn't see your answer...

Well I'm gonna share my tips here...

Well I just changed one line in the helper (line 81):

// Create full URL
$this->url = "/page/{page}";

And added a little magic line in my .htaccess file :

# Pagination
RewriteRule page/([0-9]+)$ index.php?page=$1 

Now, you pagination URL are nice :
http://mysite.local/page/xxx

That's all !

19

Re: Another Pagination helper

ZenZen wrote:

Well I'm gonna share my tips here...

Thanks ZenZen -- very helpful! I have added your enhancement to the Wiki.

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

20

Re: Another Pagination helper

The following notes explain how to use Pager.php with the default Wolf “Articles” page. Adjustments can be made from this code for paginating other subpages as well.

May I ask what changes to do when I want to make it work on other subpages, please? I try to paginate the blog subpage (I assume the link should look like "example.com/blog/page/2", but it's "example.com/page/2" which doesn't exist actually).

When I manually type the right URL, it takes me to the February 2000 archive.

Last edited by ivy046 (2011-04-18 12:56)

ivapelc.com - art & other works
Admin Themes: Deep Space | Light Blue | Green Life

21

Re: Another Pagination helper

So I guess it's a CleanURL problem, as typing example.com/blog/?page=2 takes me to the right page. Maybe the htaccess part should be more flexible? (if it's possible, it's really not my cup of tea wink)

And I'm encountering similar problem with Tagger with just one difference - links are right (example.com/blog/tag/test), but they all lead to an empty December 1999 archive big_smile

ivapelc.com - art & other works
Admin Themes: Deep Space | Light Blue | Green Life

22

Re: Another Pagination helper

Try this instead:

# Main URL rewriting.
  RewriteRule ^(.*)/page/(.*)$ index.php?WOLFPAGE=$1&page=$2 [QSA]
  RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]

instead of

# Pagination
RewriteRule page/([0-9]+)$ index.php?page=$1
Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution or see our financial report first.

23

Re: Another Pagination helper

I have been trying this too.

@mvdjkeijn
I put your fix into the htaccess. The right info displays now. But the css file is getting a 404 error. So none of the pages are being stylized.

"When you want something, all the universe conspires in helping you to achieve it." - The Alchemist

24

Re: Another Pagination helper

Nope, sorry... I still get the wrong links and now even unstyled pages sad Plus some weird behaviour in the backend, as there are "page" pages too...

Last edited by ivy046 (2011-04-19 18:11)

ivapelc.com - art & other works
Admin Themes: Deep Space | Light Blue | Green Life

25

Re: Another Pagination helper

I played around with the code a little more:

#
# Setting Wolf CMS requirements
#
# On some server configurations you may need to comment out one or more of
# the three lines below due to server limitations.
#

#php_flag magic_quotes_gpc off
#AddDefaultCharset UTF-8
#Options -Indexes +FollowSymLinks

#
# Setting rewrite rules
#

<IfModule mod_rewrite.c>
  RewriteEngine On
  # Set next line to your Wolf CMS root - if not in subdir, then just /
  RewriteBase /templates/

  # Rules to allow install sequence to test for mod_rewrite support
  RewriteRule ^install/index.html$ install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^install/index.php$ install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^install/$ install/index.php?rewrite=1 [L,QSA]
    
    
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Administration URL rewriting.
  RewriteRule ^admin(.*)$ admin/index.php?$1 [L,QSA]

  
  RewriteRule ^(.*)/page/(.*)$ index.php?WOLFPAGE=$1&page=$2 [QSA]
  

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Main URL rewriting.
 RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]



</IfModule>

As you see I put the code

RewriteRule ^(.*)/page/(.*)$ index.php?WOLFPAGE=$1&page=$2 [QSA]

right beneath the admin rewrite

The problem I am running into now is the link the pager gives me once your on the second page...

http://localhost/templates/articles/page/articles/page/1

which isn't a real page... what to do now?

Last edited by thewizardofbos (2011-04-19 19:21)

"When you want something, all the universe conspires in helping you to achieve it." - The Alchemist