user3157334
user3157334

Reputation: 79

pagination with next and previous button in codeigniter

I have tried to add pagination in CodeIgniter with below code, it comes with 1 2 3 4 5 > in this format. But I need a next and previous button to be added. Controller:

public function chooseBale($program_id = '')
{
    $config                = [];
    $config["base_url"]    = base_url() . "ginner/choosebale/" . $program_id;
    $config["total_rows"]  = $this->GinnerModel->get_count();
    $config["per_page"]    = 10;
    $config["uri_segment"] = 2;
    $choice                = $config["total_rows"] / $config["per_page"];
    $config["num_links"]   = round($choice);
    $this->pagination->initialize($config);
    $page          = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
    $data["links"] = $this->pagination->create_links();

    $data['pg_title']   = 'Choose Bale';
    $data['breadcrumb'] = ['Home' => '', 'Sale' => 'nolink', 'New Process' => 'nolink', 'Choose Bale' => 'nolink'];
    $data['left_menu']  = "left-menu-ginner.php";
    $data['program']    = $this->CommonModel->getTableWhere(PROGRAM, 'id =' . $program_id);
    $data['program_id'] = $program_id;
    $data['bale_list']  = $this->GinnerModel->baleList($program_id, $config["per_page"], $page);
    $this->load->view('common/header', $data);
    $this->load->view('ginner/choose-bale');
    $this->load->view('common/footer');
}

I have used below query in model file to get records with limit wise. Model:

public function baleList($program_id, $per_page, $page)
{
    $this->db->select('gp.id, gp.lot_no, SUM(gb.weight) AS weight, SUM(gb.staple) AS staple, SUM(gb.mic) AS mic, SUM(gb.strength) AS strength, SUM(gb.trash) AS trash, gb.color_grade');
    $this->db->from(GIN_BALES . ' gb');
    $this->db->join(GIN_PROCESS . ' gp', 'gp.id=gb.process_id');
    $this->db->where('gb.sold_status', 0);
    $this->db->where('gp.ginner_id', $this->prscr_id);
    $this->db->where('gp.program', $program_id);
    $this->db->group_by('gp.id');
    $this->db->limit($per_page, $page);
    $lot_details = $this->db->get()->result();
    $lot_details = array_column($lot_details, null, "id");
    $prs_id      = "'" . implode("','", array_keys($lot_details)) . "'";
    $bales_list  = [];
    $n           = 0;
    $this->db->select('id,process_id,press_no,weight,staple,mic,strength,trash,color_grade');
    $this->db->from(GIN_BALES);
    $this->db->where('sold_status', 0);
    $this->db->where("process_id IN (" . $prs_id . ")", null, false);
    $bales = $this->db->get()->result();
    foreach ($bales as $value) {
        if (array_key_exists($value->process_id, $lot_details)) {
            $lot_details[$value->process_id]->bales[] = $value;
        }
        $lot_details[$n]->bales = $bales;
        $n++;
    }
    return $lot_details;
}

View:

<p><?php echo $links; ?></p>

I have added this link code under the table in the view page code. The pagination works till 5 sections after that arrow comes to the first page.so I am not able to view 6th page of pagination. Is there any other correction need to do in controller or model file to display pagination in the proper way?

Upvotes: 1

Views: 1904

Answers (2)

jones
jones

Reputation: 755

Do check offical pagination document: https://www.codeigniter.com/user_guide/libraries/pagination.html#customizing-the-previous-link Here is sample.

        $config['base_url'] = base_url() . 'paging/custom';
        $config['total_rows'] = $total_records;
        $config['per_page'] = $limit_per_page;
        $config["uri_segment"] = 3;

        // custom paging configuration
        $config['num_links'] = 2;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;

        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';

        $config['first_link'] = 'First Page';
        $config['first_tag_open'] = '<span class="firstlink">';
        $config['first_tag_close'] = '</span>';

        $config['last_link'] = 'Last Page';
        $config['last_tag_open'] = '<span class="lastlink">';
        $config['last_tag_close'] = '</span>';

        $config['next_link'] = 'Next Page';
        $config['next_tag_open'] = '<span class="nextlink">';
        $config['next_tag_close'] = '</span>';

        $config['prev_link'] = 'Prev Page';
        $config['prev_tag_open'] = '<span class="prevlink">';
        $config['prev_tag_close'] = '</span>';

        $config['cur_tag_open'] = '<span class="curlink">';
        $config['cur_tag_close'] = '</span>';

        $config['num_tag_open'] = '<span class="numlink">';
        $config['num_tag_close'] = '</span>';

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

        // build paging links
        $params["links"] = $this->pagination->create_links();     

Upvotes: 0

Justin J
Justin J

Reputation: 832

See below code ,

we configure the pagination links via config library

    $this->load->library("pagination");
    $config = array();
    $config["base_url"] = base_url() . "blogs";
    $config["total_rows"] = $this->blog->getBlogCount();
    $config["per_page"] = 10;
    $config["uri_segment"] = 2;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] = '</ul>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    $config['prev_link'] = 'Previous';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';

    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';

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

Upvotes: 1

Related Questions