$offset 不适用于分页 codeigniter

2022-01-04 00:00:00 offset search php codeigniter pagination

我正在尝试在我的搜索结果页面中实现分页.我昨天问了这个问题但没有成功 Records not limited on searchpage using代码点火器分页

我现在遇到的问题有点不同.

情况

我的分页链接正在工作,我看到网址更改如下:http://example.com/home/searchresults/2(用于分页的第二页)等.但是所有的搜索结果都显示出来了.当我将限制编辑为 1 时,我会获得更多链接,因此可以正常工作.

问题

这个愚蠢的问题是什么?我认为这与我的 Total_rows

有关

我的控制器:

 函数 searchresults(){$this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );$this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );$data['breadcrumbs'] = $this->breadcrumbs->get();$match = $this->input->post('search');if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie');} else{ $match2 = '9101';}$this->load->library('分页');$limit = 4;$offset=($this->uri->segment(3)) ?$this->uri->segment(3) : 0;$config['base_url'] = base_url().'home/searchresults/';$config['total_rows'] = count($this->bedrijven_model->get_search($match, $match2, $limit, $offset ));$config['per_page'] = $limit;$config['use_page_numbers'] = TRUE;$config['num_links'] = 5;$this->分页->初始化($config);$offset = $this->uri->segment(3);$data['links'] = $this->pagination->create_links();//$data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0);/*大数作为检索所有行的限制*/$data['query_curr'] = $this->bedrijven_model->get_search($match, $match2, $config['per_page'], $this->uri->segment(3));$this->load->view('views/header');$this->load->view('views/searchresults', $data);$this->load->view('views/footer');}

我的模型:

function get_search($match, $match2, $limit, $offset=0){if(!isset($_SESSION)){session_start();};/*$string = implode($_SESSION['postcodes'], '|');$string2 = $_SESSION['searched_post_code'];*/$postcodes = (is_array($_SESSION['postcodes']) ? $_SESSION['postcodes'] : array());$postcodes[] = $_SESSION['searched_post_code'];$postcodes = array_filter(filter_var_array($postcodes, FILTER_VALIDATE_INT));$string = join(',', $postcodes);如果 (!isset($_COOKIE['cookie'])) {$query = "SELECT * FROM (`bedrijfcategorieen`)在`bedrijfcategorieen`上加入`bedrijven`.`idbedrijven` = `bedrijven`.`idbedrijven`在 `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 上加入 `categorieen`WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%'或 `Plaats` LIKE '%".$this->input->post('search')."%'或`Telefoonnummer` LIKE '%".$this->input->post('search')."%'OR `Email` LIKE '%".$this->input->post('search')."%'OR `网站` LIKE '%".$this->input->post('search')."%'或 `Profiel` LIKE '%".$this->input->post('search')."%'OR `Adres` LIKE '%".$this->input->post('search')."%'OR `Categorie` LIKE '%".$this->input->post('search')."%')AND(邮政编码输入($string))GROUP BY `Categorie`、`bedrijfcategorieen`.`idbedrijven`";$query = $this->db->query($query);$this->db->limit($limit, $offset);echo '

';echo '</pre>';$result = $query->result_array();返回 $result;}

希望有人能帮我解决这个问题.我现在为此苦苦挣扎了将近 3 个小时.

解决方案

您需要在 $query 字符串中设置限制/偏移量:

$query = "SELECT * FROM (`bedrijfcategorieen`)在`bedrijfcategorieen`上加入`bedrijven`.`idbedrijven` = `bedrijven`.`idbedrijven`在 `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 上加入 `categorieen`WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%'或 `Plaats` LIKE '%".$this->input->post('search')."%'或`Telefoonnummer` LIKE '%".$this->input->post('search')."%'OR `Email` LIKE '%".$this->input->post('search')."%'OR `网站` LIKE '%".$this->input->post('search')."%'或 `Profiel` LIKE '%".$this->input->post('search')."%'OR `Adres` LIKE '%".$this->input->post('search')."%'OR `Categorie` LIKE '%".$this->input->post('search')."%')AND(邮政编码输入($string))GROUP BY `Categorie`、`bedrijfcategorieen`.`idbedrijven`LIMIT ".$offset.", ".$limit;$result = $this->db->query($query);返回 $result->result_array();

I am trying to implement Pagination into my searchresults page. I've asked this question yesterday with no succes Records not limited on searchpage using codeigniter pagination

and the problem I have now is a bit different.

Situation

My pagination links are working and i see the url change like this: http://example.com/home/searchresults/2 (for the second page of pagination) etc. But all the searchresults are shown. When I edit the limit to 1 I get more links, so that's working properly.

Question

What could be this stupid problem? I think it has to do with my Total_rows

My controller:

    function searchresults()
    {   
        $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );            
        $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );
        $data['breadcrumbs'] = $this->breadcrumbs->get();
        $match = $this->input->post('search');
        if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie'); } else{ $match2 = '9101'; }

        $this->load->library('pagination');

        $limit = 4;
        $offset=($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $config['base_url'] = base_url().'home/searchresults/';
        $config['total_rows'] = count($this->bedrijven_model->get_search($match, $match2, $limit, $offset ));
        $config['per_page'] = $limit;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 5;   

        $this->pagination->initialize($config);
        $offset = $this->uri->segment(3);
        $data['links'] = $this->pagination->create_links();
        //$data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0); /*large number as limit to retrieve all the rows*/           
        $data['query_curr'] = $this->bedrijven_model->get_search($match, $match2, $config['per_page'], $this->uri->segment(3) );
        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
    }

My model:

function get_search($match, $match2, $limit, $offset=0)
{
    if(!isset($_SESSION)) 
    { 
    session_start();
    };
    /*
    $string = implode($_SESSION['postcodes'], '|');
    $string2 = $_SESSION['searched_post_code'];
    */
    $postcodes = (is_array($_SESSION['postcodes']) ? $_SESSION['postcodes'] : array());
    $postcodes[] =  $_SESSION['searched_post_code'];
    $postcodes = array_filter(filter_var_array($postcodes, FILTER_VALIDATE_INT));
    $string = join(',', $postcodes);

        if (!isset($_COOKIE['cookie'])) { 

            $query = "SELECT * FROM (`bedrijfcategorieen`) 
            JOIN `bedrijven` ON `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven` 
            JOIN `categorieen` ON `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 
            WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%' 
            OR `Plaats` LIKE '%".$this->input->post('search')."%' 
            OR `Telefoonnummer` LIKE '%".$this->input->post('search')."%' 
            OR `Email` LIKE '%".$this->input->post('search')."%' 
            OR `Website` LIKE '%".$this->input->post('search')."%' 
            OR `Profiel` LIKE '%".$this->input->post('search')."%' 
            OR `Adres` LIKE '%".$this->input->post('search')."%' 
            OR `Categorie` LIKE '%".$this->input->post('search')."%') 
            AND (Postcode IN ($string))

            GROUP BY `Categorie`,  `bedrijfcategorieen`.`idbedrijven`";
            $query = $this->db->query($query);
            $this->db->limit($limit, $offset);
            echo '<pre>';
            echo '</pre>';
            $result = $query->result_array();
            return $result;
      }

I hope someone can help me with this. I am struggling with this for almost 3 hours now.

解决方案

You need your limit/offset inside the $querystring:

$query = "SELECT * FROM (`bedrijfcategorieen`) 
        JOIN `bedrijven` ON `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven` 
        JOIN `categorieen` ON `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 
        WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%' 
        OR `Plaats` LIKE '%".$this->input->post('search')."%' 
        OR `Telefoonnummer` LIKE '%".$this->input->post('search')."%' 
        OR `Email` LIKE '%".$this->input->post('search')."%' 
        OR `Website` LIKE '%".$this->input->post('search')."%' 
        OR `Profiel` LIKE '%".$this->input->post('search')."%' 
        OR `Adres` LIKE '%".$this->input->post('search')."%' 
        OR `Categorie` LIKE '%".$this->input->post('search')."%') 
        AND (Postcode IN ($string)) 
        GROUP BY `Categorie`,  `bedrijfcategorieen`.`idbedrijven` 
        LIMIT ".$offset.", ".$limit;

       $result = $this->db->query($query);
       return $result->result_array();

相关文章