Part of the EllisLab Network
   
 
Pager - Pagination Widget
Posted: 17 April 2009 04:18 AM   [ Ignore ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

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.

<?php widget::run('Pager'$total_pages$current_page); ?> 
<?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($pages0$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);
    
}    
File Attachments
pager_views.zip  (File Size: 1KB - Downloads: 280)
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 17 April 2009 05:02 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Why did you go for the first generation of the function? I’m not very proud of the option settings, it’s a bit of a mess.

Profile
 
 
Posted: 17 April 2009 05:05 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Post some updates as you want xwero.

I used this because it works for me. Thanks man.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 17 April 2009 05:32 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

The last version of the function has less options because generation of the first, last, next and prev elements will not hurt the performance. The link_replacement setting is connected to the totalLinks, or in your version num_links, setting so i let that generate too.
Basically you can use all the options or none of them in the view but you don’t need to set them anymore when you call the function. I think this is more developer friendly and less ifing in the function.

An addition is the formattedLink parameter. This moves the link creation from the view to the controller if you want. This makes an array of all the generated links with two keys: nr and link. So it’s still possible to create the link in the view bu you have to use $first[‘nr’] instead of $first. Of course the idea is to use $first[‘link’].

Profile