Home > OS >  How do I make CI Pagination first page url segment always appear? First page pagination with search
How do I make CI Pagination first page url segment always appear? First page pagination with search

Time:10-21

My team and I are making a search function with pagination and the base table works fine, even the pagination works fine. The problem is when we use the search function. Upon searching, an error message pops up on the first page: Error message on the first page, although the results for the search query shows up. But when you turn to the page 2 of the search query, error message disappears and everything is normal.

This is the url of the first page where the error above shows up:

project1/index.php/configname/view?name=smith

This is the url when you turn to the next page where there are no errors:

project1/index.php/configname/view/8?name=smith

When we manually insert a zero as before the question mark, no error shows up, like so:

project1/index.php/configname/view/0?name=smith

We figured it has something to do with the uri segment not showing up on the first page, so we are looking for a solution where the zero will be the default and will be there upon loading the view/searching.

here is our config function:

public function view() 
{
    $data['title'] = 'View';

    $config = array();

    $search = $this->input->get("name");

    $config["base_url"] = base_url() . "/index.php/configname/view";
    $config["total_rows"] = $this->Users_model->get_search_count($search);
    $config["per_page"] = 8;
    $config["uri_segment"] = 3;

    if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET);
    $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);

    $config['full_tag_open'] = '<div >';        
    $config['full_tag_close'] = '</div>';        
    $config['first_link'] = 'First';        
    $config['last_link'] = 'Last';        
    $config['first_tag_open'] = '<li ><span >';        
    $config['first_tag_close'] = '</span></li>';        
    $config['prev_link'] = '&laquo';        
    $config['prev_tag_open'] = '<li ><span >';        
    $config['prev_tag_close'] = '</span></li>';        
    $config['next_link'] = '&raquo';        
    $config['next_tag_open'] = '<li ><span >';        
    $config['next_tag_close'] = '</span></li>';        
    $config['last_tag_open'] = '<li ><span >';        
    $config['last_tag_close'] = '</span></li>';        
    $config['cur_tag_open'] = '<li ><a  href="#">';        
    $config['cur_tag_close'] = '</a></li>';        
    $config['num_tag_open'] = '<li ><span >';        
    $config['num_tag_close'] = '</span></li>';

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $data['list'] = $this->Users_model->get_all($config['per_page'], $page, $search);

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

    $this->load->view('include/header', $data);
    $this->load->view('include/sidetopbar', $data);
    $this->load->view('user/viewloans', $data);
}

This is our model:

function get_all($limit, $start, $st)
{
    $sql = "select * from $this->tbl where full_name like '%$st%' order by date DESC limit " . $start . ", " . $limit;
    $query = $this->db->query($sql);
    return $query->result();
}

function get_search_count($st = NULL)
{
    if ($st == "NULL") $st = "";
    $sql = "select * from $this->tbl where full_name like '%$st%'";
    $query = $this->db->query($sql);
    return $query->num_rows();
}

This is the line in which the error was referring to for our Pagination.php on CI, but we didn't change anything from it:

if ($this->prefix !== '' OR $this->suffix !== '')
        {
            $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
        }

TL;DR: Error message shows up on the first page upon using our search function. We just want to make it disappear as the search queries properly work, just the error makes us mad.

CodePudding user response:

in this section

if ($this->prefix !== '' OR $this->suffix !== '')
    {
        $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
    }

check if $this->suffix is null set it to 1

if(!is_numeric($this->suffix)){
     $this->suffix = 1;
}

CodePudding user response:

CodeIgniter seems to treat the search query string as a page number suffix: when it gets the page number from the url, it removes this suffix from the page number. When the page number is missing from the url, it tries to remove this suffix from a null value, and because of the more strict type checking in php 8 this gives the error. (I don't get this error with CI3 on php 7.4.)

It's not in the documentation (found here: https://github.com/bcit-ci/CodeIgniter/pull/4384 ), but you can also set the current page for the Pagination library manually using $config['cur_page'] = ... ;

If you set this to something that isn't considered "empty" by php, CodeIgniter will use this page number instead of getting the one from the url. And if you set it to a page number that does not make sense, for example, -1, CodeIgniter will use 0 instead. So you could try setting it to -1 when the segment is missing:

$config['cur_page'] = $this->uri->segment(3) ?: -1;

Not sure if this triggers other errors on php 8 though!

  • Related