Genrate Custom Pagination Link

Hello Guys ,

Today I am sharing about pagination. If you are looking pagination script that only generate paging link then this function can help you. You can use this function in any MVC with your custom query. You just need to call this method and it will return the paging link.

Below is the argument description:-

$page= current page(by default 0)

$num_rows= total record.

$results_per_page=how many records you want per page.

$each_direction = direction

$fieldname= url variable by which you will call pagin.

function pagination_links($page, $num_rows, $results_per_page, $each_direction = 5, $fieldname = 'page')
{


$word_for_previous = 'prev';
$word_for_next = 'next';
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
if($total_pages < 2)
{
return null;
}
$page = ((is_numeric($page)) and ($page >= 1) and ($page <= $total_pages)) ? (int)$page : 1 ;
$output = null;
$query_string = '';
foreach($_GET as $k => $v)
{
if($k != $fieldname)
{
$query_string .= get_magic_quotes_gpc() ?
urlencode(stripslashes($k)).'='.urlencode(stripslashes($v)).'&amp;':
urlencode($k).'='.urlencode($v).'&amp;';
}
}
if($page > 1)
{
$output .= '<a href="'.URL::current().'?'.$query_string.$fieldname.'='.($page - 1).'">'.$word_for_previous.'</a>'."\n | \n";
}
for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
{
if(($i > 0) and ($i <= $total_pages))
{
$output .= isset($spacer) ? $spacer : null ;
$spacer = ' | '."\n";
if($page != $i)
{
$output .= '<a href="'.URL::current().'?'.$query_string.$fieldname.'='.$i.'">'.$i.'</a>'."\n";
}
else
{
$output .= '<span>' . $i . "</span>\n";
}
}
}
if($page < $total_pages)
{
$output .= " | \n".'<a href="'.URL::current().'?'.$query_string.$fieldname.'='.($page + 1).'">'.$word_for_next.'</a>';
}
return "\n<p class=\"pagination-links\">\n$output\n</p>\n";
}


Use it and cheers 🙂

Share This Post

Recent Articles

Leave a Reply

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

© 2017 Networkocean. All rights reserved. Site Admin · Entries RSS · Comments RSS
Powered by WordPress · Designed by Theme Junkie