Welcome Guest, Not a member yet? Register   Sign In
Pager - Pagination Widget
#1

[eluser]wiredesignz[/eluser]
Based on code provided by xwero, this is an alternative to the pagination library, as a pagination widget. The pager views are attached to this post as a zip file.
Code:
<?php widget::run('Pager', $total_pages, $current_page); ?>
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pager extends Widget
{
    function run($pages = 0, $page = 1) {        
        $this->create_links($page, $pages, '_basic', current_url(), array('num_links' => 3, 'link_replacements', 'first', 'prev', 'next', 'last'));
    }
    
    function create_links($page_nr, $page_total, $view, $base_url = '', $links = array()) {    
         $current_key = 0;
         $pages = range(1, $page_total);
    
         // reset base_url
         $base_url = preg_replace('/\/page\/\d+$/', '', rtrim($base_url, '/')).'/page/';

         // add url to the pages array items
         foreach($pages as $key => $page)
         {
             if($page != $page_nr) {
                 $pages[$key] = array($base_url.$page, $page);
             } else {
                 $current_key = $key;
             }
         }
        
         // set extra data needed in the view
         if( ! empty($links)) {
            
            if(in_array('prev', $links) AND $current_key > 0) {
                $data['prev'] = $pages[$current_key - 1];
             }
            
            if(in_array('next', $links) AND $current_key < count($pages) - 1) {
                $data['next'] = $pages[$current_key + 1];
            }
            
            if(in_array('first', $links)) {
                $data['first'] = (is_array($pages[0])) ? $pages[0] : array($base_url.$pages[0], $pages[0]);
            }
            
            if(in_array('last', $links)) {
                $temp = end($pages);
                $data['last'] = (is_array($temp))? $temp : array($base_url.$temp, $temp);
            }
            
            $link_replacements = (bool)(in_array('link_replacements', $links));
            
            if(isset($links['num_links'])) {  
                $val = $links['num_links'];  
                if($pages[$current_key] <= $val) {
                    $pages = array_slice($pages, 0, $current_key + $val + 1);
                    if($link_replacements) {
                        $data['missing_next'] = TRUE;
                    }
                } elseif($pages[$current_key] + $val > $page_total) {
                    $pages =  array_slice($pages, $current_key - $val);
                    if($link_replacements) {
                        $data['missing_prev'] = TRUE;
                    }
                } else {
                    $offset = $current_key - $val;
                    $length = ($val * 2) + 1;
                    $pages = array_slice($pages, $offset, $length);
                    if($link_replacements) {
                        if($pages[$length-1][1] < $page_total) {
                              $data['missing_next'] = TRUE;
                        }
                        if($pages[$offset][1] != 1) {
                              $data['missing_prev'] = TRUE;
                        }
                    }
                }
           }
       }
       $data['urls'] = $pages;
       $this->render('pager/'.$view, $data);
    }    
}


Messages In This Thread
Pager - Pagination Widget - by El Forum - 04-17-2009, 02:18 AM
Pager - Pagination Widget - by El Forum - 04-17-2009, 03:02 AM
Pager - Pagination Widget - by El Forum - 04-17-2009, 03:05 AM
Pager - Pagination Widget - by El Forum - 04-17-2009, 03:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB