1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
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;
} |
Partager