Codeigniter Pagination Example (Query String )

Before Going through the complete article I assume that you have a basic idea of CodeIgniter,

Controller Code:

public function user_location($start_from = 0) {

        $offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 0);

        $config = array();
        $config["total_rows"] = $this->Reports_model->user_record_count();
        $config['per_page'] = 4;
        $config['full_tag_open'] = '
        $config['full_tag_close'] = '
';        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        $config["base_url"] = base_url() . "reports/user_location";
        $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['first_url'] = base_url() . 'reports/user_location/?' . http_build_query($_GET, '', "&");

        $this->pagination->initialize($config);

        $data['links'] = $this->pagination->create_links();

        if (!empty($start_from)) {
            $start = $config['per_page'] * ($start_from - 1);
        } else {
            $start = 0;
        }

        $param = array();
        $param = $_GET;

        $data['results'] = $this->Reports_model->fetch_user($config["per_page"], $start, $param);


     
        $data['header'] = $this->load->view('main_header', '', true);
        $data['menu'] = $this->load->view('main_menu', '', true);
        $data['footer'] = $this->load->view('main_footer', '', true);

        $this->load->view('user_list', $data);
    }

Model Code :


public function fetch_user($limit, $start, $param) {
        $return_result = array();
        $this->db->select('user.mobile,user_type_master.name,user.city_name');
        $this->db->from('user');
        $this->db->join('user_type_master', 'user_type_master.id = user.user_type_id', 'LEFT');

        if (isset($param['user_type']) && !empty($param['user_type'])) {
            $this->db->where('user.user_type_id', $param['user_type']);
        }

        $this->db->limit($limit, $start);

        $result = $this->db->get();

        if ($result && $result->num_rows() > 0) {
            $return_result = $result->result_array();
            foreach ($return_result as $key => $value) {
                if (isset($value ['profile_image']) && !empty($value ['profile_image'])) {
                    $return_result [$key] ['profile_image'] = DESIGNS_MEDIA_URL . $value ['profile_image'];
                }
            }
        }

        return $return_result;

    }


View Code :

I don't think its required here.

No comments:

Post a Comment