Saturday, July 5, 2014

Pagination for data on multiple tables in Codeigniter

Codeigniter comes with a great pagination library. Here, you just need to define the start and limit number.But if you have data over multiple tables and you have know way to join those tables. Then how would be the scenario?

You can get the total data into an array. Then slice your array according to your pagination limit.
foreach ($group_all as $group) {
    $data_form_model[$a] = $this->admin_model->all_number_model($group->group_name);
    $data_form_model_group_name[$a] = $group->group_name;
    $a++;
}

$final_array = array();
foreach ($data_form_model as $val) {
    foreach ($val as $val2) {
        $final_array[] = $val2;
    }
}

$config = array();
$config["base_url"] = base_url() . "admin/all_number/";
$config["total_rows"] = count($final_array);
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config['num_links'] = 3;

$config['full_tag_open'] = '
    '; $config['full_tag_close'] = '
'; $config['first_link'] = '«'; $config['first_tag_open'] = ''; $config['last_link'] = '»'; $config['last_tag_open'] = ''; $config['next_link'] = '→'; $config['next_tag_open'] = ''; $config['prev_link'] = '←'; $config['prev_tag_open'] = ''; $config['cur_tag_open'] = '
  • '; $config['cur_tag_close'] = '
  • '; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $data_form_model = array_slice($final_array, $page , $config["per_page"]); $data["links"] = $this->pagination->create_links();




    List of my works:

    Opencart Extensions:

    1. Product Based Quantity Wise Shipping: Find it here.
    2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
    3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
    4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
    5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
    6. Formcaptcha - add captcha on the register page: Find it here.

    My Books:

    1. OpenCart 1.4 Template Design Cookbook.
    2. Joomla Mobile Development Beginners Guide