Welcome Guest, Not a member yet? Register   Sign In
Pagination plugin
#1

[eluser]xwero[/eluser]
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

[eluser]Phil Sturgeon[/eluser]
Looking good xwero. I always hate pagination so anything to make the CI lib easier to work with is welcomed.
#3

[eluser]xwero[/eluser]
To set the record straight, the plugin is a library replacement.
#4

[eluser]xwero[/eluser]
I made a universal function from my pagination plugin
Code:
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.
Code:
<ul>
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $first ?&gt;</a></li>&lt;?php if(isset($prev)): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $prev ?&gt;</a></li>&lt;?php endif; if(isset($missing_prev)): ?&gt;
<li>...</li>&lt;?php endif; foreach($pages as $page): if($page != $currentPage): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $page ?&gt;</a></li>&lt;?php else: ?&gt;
<li>&lt;?php echo $page ?&gt;</li>&lt;?php endif; endforeach; if(isset($missing_next)): ?&gt;
<li>...</li>&lt;?php endif; if(isset($next)): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $next ?&gt;</a></li>&lt;?php endif ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $last ?&gt;</a></li>
</ul>
To get the viewpath in CI you have to add this function
Code:
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

[eluser]abada[/eluser]
please can u explain more for how to use it

where models ?

thanks
#6

[eluser]xwero[/eluser]
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

[eluser]wiredesignz[/eluser]
This is a very nice Pagination Widget xwero. Good work man.
#8

[eluser]abada[/eluser]
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-gui...ation.html

or what ? ...

please ....
#9

[eluser]xwero[/eluser]
wiredesignz now that you called it a widget i'm moving the view out of the function Smile
Code:
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
Code:
// controller
$this->load->view('pagination',getLinks($this->model->row_count(),$this->uri->segment(3)));
// views/pagination.php
<ul>
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $first ?&gt;</a></li>&lt;?php if(isset($prev)): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $prev ?&gt;</a></li>&lt;?php endif; if(isset($missing_prev)): ?&gt;
&lt;!-- notice the change from $currentPage to $current_page --&gt;
<li>...</li>&lt;?php endif; foreach($pages as $page): if($page != $current_page): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $page ?&gt;</a></li>&lt;?php else: ?&gt;
<li>&lt;?php echo $page ?&gt;</li>&lt;?php endif; endforeach; if(isset($missing_next)): ?&gt;
<li>...</li>&lt;?php endif; if(isset($next)): ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $next ?&gt;</a></li>&lt;?php endif ?&gt;
<li><a href="&lt;?php /* url creation here */ ?&gt;">&lt;?php echo $last ?&gt;</a></li>
</ul>
If you want to add the pagination markup to the actual page view.
Code:
// controller
$data['pagination'] = getLinks(10,$this->uri->segment(3),site_url('controller/method/%d'));
$this->load->view('content',$data);
For abada Wink
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.




Theme © iAndrew 2016 - Forum software by © MyBB