Custom Pagination for CI

I have tried Codeigniter pagination for some grid but its bump. So I decide to write own pagination for Codeigniter you can also use it for Core PHP with some alteration. So here is the code:

/*
* @param
* total = number of records
* diff = number of records per page to display
* base_url = url of the page where record is being display
* page = current page displaying
*/
public function pagination($total, $diff, $base_url,$page) {
$output = '

';
$output .= '
Display ';
$output .= form_dropdown('diff', array(15, 30, 45, 60));
$output .= 'Replies';
$output .= '
Next';
$output .= '';
$output .= '
';
$output .= '

';
$output .= 'Previous
';
//generate pagination for specific page
$i = ($page>0 && $page<5)?0:$page-5;
$j = 0;
$total_page = ($page<$total && ($page5))?'...':'';
$page = ($page>0 && $page<5)?0:$page-5;
for (; $i <= $total; $i+=$diff) { $j++; $page++; if ($page >= $total/$diff){
break;
}
if ($j < 10) { // generate pagination with sorting through field is on if ($this->CI->input->get('field') != "")
{
$total_page .= ' ' . $page . ' ';
}
//generate simple pagination link
else
{
$total_page .= ' ' . $page . ' ';
}
}
else
{
$total_page .= '...';
break;
}
}
$output .= $total_page;
$output .= '

';

return $output;
}

Hope this will help, Happy coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s