ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Pagination plugin

January 09, 2009 8:21am

Subscribe [6]
  • #1 / Jan 09, 2009 8:21am

    xwero

    4145 posts

    There are already a few pagination plugins out there, Flexible Paging Plugin and Paging Simplified can be found in the wiki and there will be more if you do a search. But they didn’t do it for me so i created my own.

    The main reason why i made the pagination a plugin and not a library is because i wanted to use CI functionality. IMO should a library be framework independent. And the CI pagination only has one functional method, create_links, so it’s perfect to make a plugin out of it.

    Why you should use it:

    - It separates the output from the link creation, this means no template settings.
    - It’s easier to finetune the link output.
    - Because each time the links are created you donate a cent to a good cause, yes it’s infected with goodware.

    The zip file contains the plugin file, a few view files and the plugin documentation.

    All comments and bug reports are welcome in this thread.

  • #2 / Jan 09, 2009 10:30am

    Phil Sturgeon

    2889 posts

    Looking good xwero. I always hate pagination so anything to make the CI lib easier to work with is welcomed.

  • #3 / Jan 09, 2009 10:35am

    xwero

    4145 posts

    To set the record straight, the plugin is a library replacement.

  • #4 / Mar 05, 2009 5:05am

    xwero

    4145 posts

    I made a universal function from my pagination plugin

    function getLinks($viewPath,$currentPage,$totalPages,$totalLinks=0)
        {
            if( ! file_exists($viewPath))
            {
              return false;
            }
            
            $pages = range(1,$totalPages);
            $current_key = $currentPage-1;
    
            if($current_key > 0){ $prev = $pages[$current_key-1]; }
    
            if($current_key < count($pages)-1){ $next = $pages[$current_key+1]; }
    
            $first = $pages[0];
    
            $last = end($pages);
    
            if($totalLinks != 0)
            {
                if($pages[$current_key] <= $totalLinks)
                {
                    $pages = array_slice($pages,0,$current_key+$totalLinks+1);
                    $missing_next = TRUE;
                }
                elseif($pages[$current_key]+$totalLinks > $totalPages)
                {
                    $pages =  array_slice($pages,$current_key-$val);
                    $missing_prev = TRUE;
                }
                else
                {
                    $offset = $current_key-$totalLinks;
                    $length = ($totalLinks*2)+1;
                    $pages = array_slice($pages,$offset,$length);
                    if($pages[$length-1] < $totalPages){ $missing_next = TRUE; }
                    if($pages[$offset] != 1){ $missing_prev = TRUE; }
                }
            }
    
            return include($viewPath);
        }

    I left out the base url because you can build the url in the view file. Following is a view file with all the variables that can be used.

    <ul>
    <li><a href="http://&lt?php"><?php echo $first ?></a></li><?php if(isset($prev)): ?>
    <li><a href="http://&lt?php"><?php echo $prev ?></a></li><?php endif; if(isset($missing_prev)): ?>
    <li>...</li><?php endif; foreach($pages as $page): if($page != $currentPage): ?>
    <li><a href="http://&lt?php"><?php echo $page ?></a></li><?php else: ?>
    <li><?php echo $page ?></li><?php endif; endforeach; if(isset($missing_next)): ?>
    <li>...</li><?php endif; if(isset($next)): ?>
    <li><a href="http://&lt?php"><?php echo $next ?></a></li><?php endif ?>
    <li><a href="http://&lt?php"><?php echo $last ?></a></li>
    </ul>

    To get the viewpath in CI you have to add this function

    function viewPath($view)
    {
       $CI =& get_instance();
       return $CI->load->_ci_view_path . $view . EXT;
    }


    The output of the functions doesn’t need to be echo-ed, if you do echo it you will see 0 or 1 appear because the echo outputs the include function result. If you want the output buffering functions can be added to prevent this.

  • #5 / Mar 26, 2009 6:42pm

    abada

    36 posts

    please can u explain more for how to use it

    where models ?

    thanks

  • #6 / Mar 26, 2009 6:57pm

    xwero

    4145 posts

    the only parameter that could be powered by a model is totalPages. The plugin doesn’t need to know how many rows are getting displayed, it just has to take care of the link generation.

  • #7 / Mar 26, 2009 7:54pm

    wiredesignz

    2882 posts

    This is a very nice Pagination Widget xwero. Good work man.

  • #8 / Mar 27, 2009 3:55am

    abada

    36 posts

    thanks xwero for ur help but really i don`t know how to use it ..

    i just copy pagination_pi.php in “system/plugins” ...

    and now what after that i mean (where controller and model and view )...

    do i do in user guide
    http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

    or what ? ...

    please ....

  • #9 / Mar 27, 2009 5:13am

    xwero

    4145 posts

    wiredesignz now that you called it a widget i’m moving the view out of the function 😊

    function getLinks($totalPages,$currentPage=0,$formattedLink='',$totalLinks=0)
        {
            if(empty($currentPage)){ $currentPage = 1; }
            
            $output['current_page'] = $currentPage;
    
            $pages = range(1,$totalPages);
            $current_key = $currentPage-1;
    
            if($current_key > 0){ $output['prev'] = $pages[$current_key-1]; }
    
            if($current_key < count($pages)-1){ $output['next'] = $pages[$current_key+1]; }
    
            $output['first'] = $pages[0];
    
            $output['last'] = end($pages);
    
            if($totalLinks != 0)
            {
                if($pages[$current_key] <= $totalLinks)
                {
                    $pages = array_slice($pages,0,$current_key+$totalLinks+1);
                    $output['missing_next'] = TRUE;
                }
                elseif($pages[$current_key]+$totalLinks > $totalPages)
                {
                    $pages =  array_slice($pages,$current_key-$val);
                    $output['missing_prev'] = TRUE;
                }
                else
                {
                    $offset = $current_key-$totalLinks;
                    $length = ($totalLinks*2)+1;
                    $pages = array_slice($pages,$offset,$length);
                    if($pages[$length-1] < $totalPages){ $output['missing_next'] = TRUE; }
                    if($pages[$offset] != 1){ $output['missing_prev'] = TRUE; }
                }
            }
    
            if($formattedLink != '')
            {
               $output['first'] = array('nr'=>$output['first'],'link'=>sprintf($formattedLink,$output['first']));
    
               $output['last'] = array('nr'=>$output['last'],'link'=>sprintf($formattedLink,$output['last']));
    
               if(isset($output['prev']))
               {
                   $output['prev'] = array('nr'=>$output['prev'],'link'=>sprintf($formattedLink,$output['prev']));
               }
    
               if(isset($output['next']))
               {
                   $output['next'] = array('nr'=>$output['next'],'link'=>sprintf($formattedLink,$output['next']));
               }
    
               foreach($pages as $i=>$page)
               {
                   $pages[$i] = array('nr'=>$page,'link'=>sprintf($formattedLink,$page));
               }
            }
    
            $output['pages'] = $pages;
    
            return $output;
        }

    Next to the removal of the view i have added a current page check and a new parameter formattedLink to create links in the controller instead of the view.

    So the new usage examples are

    // controller
    $this->load->view('pagination',getLinks($this->model->row_count(),$this->uri->segment(3)));
    // views/pagination.php
    <ul>
    <li><a href="http://&lt?php"><?php echo $first ?></a></li><?php if(isset($prev)): ?>
    <li><a href="http://&lt?php"><?php echo $prev ?></a></li><?php endif; if(isset($missing_prev)): ?>
    <!-- notice the change from $currentPage to $current_page -->
    <li>...</li><?php endif; foreach($pages as $page): if($page != $current_page): ?>
    <li><a href="http://&lt?php"><?php echo $page ?></a></li><?php else: ?>
    <li><?php echo $page ?></li><?php endif; endforeach; if(isset($missing_next)): ?>
    <li>...</li><?php endif; if(isset($next)): ?>
    <li><a href="http://&lt?php"><?php echo $next ?></a></li><?php endif ?>
    <li><a href="http://&lt?php"><?php echo $last ?></a></li>
    </ul>

    If you want to add the pagination markup to the actual page view.

    // controller
    $data['pagination'] = getLinks(10,$this->uri->segment(3),site_url('controller/method/%d'));
    $this->load->view('content',$data);

    For abada 😉
    The function returns an array that is generated based on the parameter input.

    totalPages is the maximum amount of links that is going to be created.

    currentPage is the segment/variable(for example session item) that defines the pagenumber.

    formattedLink creates links if you want them this will change the output of the link parts from a numeric value to an array with the keys nr and link. It uses sprintf to format the links.

    totalLinks is the amount of links you want to be visible, first, last, next and prev links excluded. The amount center is the current page.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases