2013-05-28 71 views
0

我想實現分頁到我的searchresults頁面。我昨天問過這個問題沒有成功Records not limited on searchpage using codeigniter pagination

和我現在的問題有點不同。

形勢

我的分頁鏈接都工作,並且我看到URL的變化是這樣的:http://example.com/home/searchresults/2(用於分頁的第二頁)等 但所有的SearchResult所顯示。當我將限制編輯爲1時,我得到更多鏈接,因此工作正常。

問題

可能是什麼這個愚蠢的問題呢?我認爲這與我的TOTAL_ROWS

我控制器做:

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'); 
    } 

我的模型:

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; 
     } 

我希望有人能幫助我與此有關。我現在掙了近3個小時。

回答

1

你需要你的極限/在$query字符串內偏移:

$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(); 
+0

我得到這個錯誤http://i.imgur.com/FxmjLVA.png只是看看它。 –

+0

嘗試將GROUP BY設置爲低於GROUP BY - 查看更新的代碼。 – Mudshark

+0

現在幾乎相同的錯誤。 http://i.imgur.com/6jNQz6J.png –

0

$this->db->limit僅適用於活動記錄。總之,您的查詢更改爲此

$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"; 
     $query = $this->db->query($query); 

一件事,沒有必要使用$這 - > DB->限制