Search code examples
codeigniterpaginationcodeigniter-2codeigniter-urlcodeigniter-routing

How to create pagination using dropdown option in codeigniter


I want to create a pagination using codeigniter.problem is i can't show my record according to the value from dropdown option list.Plz help.

here is my controller code:

    public function index($offset = 0)
    {
        $data['title'] = "Basic CRUD Function With Codeigniter";

        if (!empty($post['limit'])) {

            $limit = $this->input->post('limit');
        }
        else{
            $limit = 1;
        }

        $offset = $this->uri->segment(3);

        $records = $this->crud_mdl->get_paged_list($limit, $offset);

        $config['base_url'] = base_url().'/crud/index/';
        $config['total_rows'] = $this->crud_mdl->count_all();
        $config['per_page'] = $limit;

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

        $data['limit'] = $this->input->post('limit');
        $data['pagination'] = $this->pagination->create_links();

        $this->load->library('table');
        $this->table->set_heading('ID','NAME','E-MAIL','PHONE','ACTION');

        foreach ($records as $record) {

            $this->table->add_row($record->id, $record->name, $record->email, $record->phone,

                anchor('crud/viewCrud/'.$record->id,'view').' '.anchor('crud/editCrud/'.$record->id,'edit').' '.anchor('crud/delete/'.$record->id,'delete')
                );
        }

        $data['table'] = $this->table->generate();

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

here is my model:

    public function count_all()
    {
        return $this->db->count_all('test');
    }
    public function get_all_data()
    {
        return $this->db->get('test')->result();
    }
    public function get_paged_list($limit, $offset)
    {
        return $this->db->get('test', $limit, $offset)->result();
    }

here is my view:

    <div>
    <h1>Simple CRUD Application</h1>
    <div><?php echo $pagination;?></div>
    <?php echo form_open('crud/index');?>
    <select name="limit" onchange="form.submit()">
        <option value="1" <?php if ($limit == 1){ echo "selected='selected'";} ?>>1</option>
        <option value="2" <?php if ($limit == 2){ echo "selected='selected'";}?> >2</option>
        <option value="4" <?php if ($limit == 4){ echo "selected='selected'";}?> >4</option>
    </select>
    <?php echo form_close();?>
    <div><?php echo $table; ?></div>
    <br />
    <?php echo anchor('crud/addCrud','Add CRUD'); ?>
</div>

Solution

  • $post is not defined, and probably you mean $_POST, which can also be used as $this->input->post(). So replace:

    if (!empty($post['limit'])) {
    

    It should be:

    if (!empty($this->input->post('limit'))) {