2
votes

First of all, I am new at CodeIgniter and I have searched and found a lot of the similar problems that I'm having and have tried all of the solutions but none works. I have a search input that display my data with pagination, the problem comes when there is a get params set in the url for the second page, the data displayed is of the second page but the pagination number link active is still on page 1. I will just display the related codes (I have construct, index, load and etc placed correctly).

models/model.php

public function get_results($search_term='default', $limit, $start) {
    $this->db->select('*');
    $this->db->from('table');
    $this->db->like('title', $search_term');
    $this->db->order_by('PID DESC');
    $query = $this->db->get('', $limit, $start);
    return $query->result();
}

controllers/controller.php

public function execute_search() {
    // Retrieve the posted search term
    $search_term = $this->input->post('search');

    // Retrieve get variables if goes to second page
    // url example: localhost:/index/main/execute_search/query/15
    // If segment is 4, the third segment is the query get variable passed through link
    $search_term = ($this->uri->segment(4)) ? $this->uri->segment(3) : $search_term;

    // Pagination settings
    $config['base_url'] = site_url('main/execute_search/' . $search_term . '/');
    $this->db->like('title', $search_term);
    $this->db->from('table');
    $totalrows = $this->db->count_all_results();
    $config['total_rows'] = $totalrows;
    $config['per_page'] = '15';
    $config['uri_segment'] = 3;
    $choice = $config['total_rows'] / $config['per_page'];
    $config['num_links'] = floor($choice);

    // config for bootstrap pagination class integration
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

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

    // If segment is 4, current page is the fourth segment
    $data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

    // Use a model to retrieve the results
    $data['datalist'] = $this->model->get_results($search_term, $config['per_page'], $data['page']);

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

    // Pass the results to the view
    $this->load->view('main', $data);

views/main.php

<!DOCTYPE HTML>
<html>
<head>
    <!-- Bootstrap css -->
    <link rel='stylesheet' href='<?= base_url('assets/css/bootstrap.css'); ?>'></link>
</head>
<body>
<?php

    echo form_open('main/execute_search');

    echo form_input(array('name'=>'search'));

    echo form_submit('search_submit', 'Search');

    // result
    for($i = 0; $i < count($datalist); $i++) {
        echo $datalist[$i]->title;
    }

    // Pagination
    echo $pagination

?>
</body>
</html>

localhost/index/main/execute_search

enter image description here

localhost/index/main/execute_search/15

enter image description here

localhost/index/main/execute_search/query/15

enter image description here

localhost/index/main/execute_search/query/30

enter image description here

The problem came when I added the get params in the url, please help me with this. Thank you so much in advance.

1

1 Answers

1
votes

Move :

$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

Above:

$config['base_url'] = site_url('main/execute_search/' . $search_term . '/');

Then Change:

$config['base_url'] = site_url('main/execute_search/' . $search_term . '/');

to

$config['base_url'] = site_url('main/execute_search/'.$data['page'].'/' . $search_term . '/');

Also Change:

$search_term = ($this->uri->segment(4)) ? $this->uri->segment(3) : $search_term;

to

$search_term = ($this->uri->segment(5)) ? $this->uri->segment(5) : $search_term;