CodeIgniter Forums

Full Version: Custom Pagination
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I use a custom pagination library rather than codeigniter inbuilt one.

My question is. I have a $filter = array() and would like to know if my off set $start variable is safe and if there would be any thing better.

Also other question is made it secure pagination on controller?

It out puts url like http://localhost/project-1/admin/category?&sort=category_name&order=ASC

PHP Code:
public function get_categories($data = array()) {
$this->db->select('*');
$this->db->from($this->db->dbprefix "category");
$this->db->order_by('category_name'$data['order']);
$this->db->offset($data['start']);
$this->db->limit($data['limit']);
$query $this->db->get();

if (
$query->num_rows() > 0) {
return 
$query->result_array();
} else {
return 
false;
}



PHP Code:
public function index() {
$this->get_list();
}

public function 
get_list() {
$this->document->setTitle('Category List');

$this->load->library('custom_pagination');

$get_sort $this->input->get('sort');
$get_order $this->input->get('order');
$get_page $this->input->get('page');

if (isset(
$get_sort)) {
$sort $get_sort;
} else {
$sort 'name';
}

if (isset(
$get_order)) {
$order $get_order;
} else {
$order 'ASC';
}

if (isset(
$get_page)) {
$page $get_page;
} else {
$page 1;
}

$url '';

if (isset(
$get_sort)) {
$url .= '?&sort=' $get_sort;
}

if (isset(
$get_order)) {
$url .= '&order=' $get_order;
}

if (isset(
$get_page)) {
$url .= '&page=' $get_page;
}

$data['breadcrumbs'] = array();

$data['breadcrumbs'][] = array(
'text' => 'Home',
'href' => site_url('admin/dashboard')
);

$data['breadcrumbs'][] = array(
'text' => 'Banner Edit',
'href' => site_url('admin/category' $url)
);

$data['heading_title'] = 'Category List';

$data['column_name'] = 'Category Name';
$data['column_action'] = 'Action';

$data['add'] = site_url('admin/category/add');

$data['categories'] = array();

$filter_data = array(
    
'sort'  => $sort,
    
'order' => $order,
    
'start' => ($page 1) * config_item('limit_admin'),
    
'limit' => config_item('limit_admin')
);

$category_total $this->model_category->get_category_total();

$results $this->model_category->get_categories($filter_data);

if (!empty(
$results)) { 
    foreach(
$results as $result) {
        
$data['categories'][] = array(
            
'category_id' => $result['category_id'],
            
'parent_category_id' => $result['parent_category_id'],
            
'category_name' => ucfirst($result['category_name']),
            
'parent_category_name' => $this->model_category->get_parent_name($result['parent_category_id']), 
            
'edit' => anchor('admin/category/edit/' $result['category_id'] .'/'$url'Edit')
        );
    }
}
        
$session_flashdata $this->session->flashdata('success');

if (isset(
$session_flashdata)) {
    
$data['success'] = $session_flashdata;

    unset(
$session_flashdata);
} else {
    
$data['success'] = '';
}

$input_post $this->input->post('selected');

if (isset(
$input_post)) {
    
$data['selected'] = (array)$input_post;
} else {
    
$data['selected'] = array();
}

$url '';

if (
$order == 'ASC') {
    
$url .= '&order=DESC';
} else {
    
$url .= '&order=ASC';
}

if (isset(
$get_page)) {
    
$url .= '&page=' $get_page;
}

$data['sort_name'] = site_url('admin/category' '?&sort=name' $url);

$url '';

if (isset(
$get_sort)) {
    
$url .= '?&sort=' $get_sort;
}

if (isset(
$get_order)) {
    
$url .= '&order=' $get_order;
}

$pagination = new Custom_pagination();
$pagination->total $category_total;
$pagination->page $page;
$pagination->limit config_item('limit_admin');

if (
$get_sort == TRUE) {
    
$pagination->url site_url('admin/category' $url '&page={page}');
} else {
    
$pagination->url site_url('admin/category' $url '?&page={page}');
}

$data['pagination'] = $pagination->render();

$text_pagination 'Showing %d to %d of %d (%d Pages)';

$data['results'] = sprintf($text_pagination, ($category_total) ? (($page 1) * config_item('limit_admin')) + 0, ((($page 1) * config_item('limit_admin')) > ($category_total config_item('limit_admin'))) ? $category_total : ((($page 1) * config_item('limit_admin')) + config_item('limit_admin')), $category_totalceil($category_total config_item('limit_admin')));

$data['sort'] = $sort;
$data['order'] = $order;


$data['header'] = Modules::run('admin/common/header/index');
$data['navbar'] = Modules::run('admin/common/navbar/index');
$data['footer'] = Modules::run('admin/common/footer/index');

$this->load->view('template/category/category_list_view'$data);