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://<?php"><?php echo $first ?></a></li><?php if(isset($prev)): ?>
<li><a href="http://<?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://<?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://<?php"><?php echo $next ?></a></li><?php endif ?>
<li><a href="http://<?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.