2016-10-04 67 views
0

我的分頁搜索結果不起作用。它顯示的結果,但分頁使其隨機。請幫幫我。搜尋工作,但它不能適用分頁,它變得越來越複雜搜索結果分頁工作不正常...搜索正在工作,但分頁導致codeigniter中的錯誤

search_products_model

<?php 
class search_products_model extends CI_Model { 

    public function __construct() 
     { 
       parent::__construct(); 
     } 
    public function get_results($search_term,$limit, $offset) 
    { 
     // Use the Active Record class for safer queries.  
     $this->db->like('name',$search_term); 

     // Execute the query. 
     $query = $this->db->get('tbl_products',$limit, $offset);  

     if ($query->num_rows() > 0) 
     { 
      return $query->result_array(); 
     } 
     else 
     { 
    return FALSE; 
     } 
     // Return the results.    
    } 

     function get_products_id($id) 
     { 
     $this->db->where('prod_id',$id); 
      $query = $this->db->get('tbl_products'); 
      return $query->result(); 
     }   
} 
?> 

controller 

<?php 
class search_products extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form','url')); 
     $this->load->library(array('session', 'form_validation', 'email','pagination')); 
     $this->load->database(); 
     $this->load->model('search_products_model'); 
    } 

    function index() 
    { 
     $this->search_products(); 
     $this->load->view('search_products_view'); 
    } 
    function display_registration_form() 
    {   
    $this->load->view('search_products_view'); 
    } 

    function execute_search($offset = 0) 
    {  
     $config['base_url'] = '/mehmaa/index.php/search_products/execute_search/'; 
     $config['total_rows'] = $this->db->count_all_results('tbl_products'); 
     $config['per_page'] = 6; 
     $config['next_link'] = 'Next'; 
     $config['prev_link'] = 'Previous';    

     $this->pagination->initialize($config); 
     // Retrieve the posted search term. 
     $id = $this->input->post('id'); 
     $search_term = $this->input->post('term'); 
     // Use a model to retrieve the results. 
     $data['results'] = $this->search_products_model->get_results($search_term,$config['per_page'], $offset); 

     if (empty($data['results'])) 
    {      
       $this->load->view('search_products_view'); 
       $this->session->set_flashdata('message', 'No results found');  
    } 
     else{ 
     // Pass the results to`enter code here` the view.    
     $this->load->view('search_results',$data);   
     } 
     } 

回答

0

喜在這個環節你不檢查搜索條件

$config['total_rows'] = $this->db->count_all_results('tbl_products'); 

改變這一行

$config['total_rows'] = $this->db->count_all_results($search_term); 

並寫出正確的查詢

0
$config['total_rows'] = $this->db->count_all_results('tbl_products'); 

在這個文件中的問題,你需要編寫另一個查詢,將通過搜索收集結果沒有偏移和限制。 例如

function search($text, $offset, $limits){ 
    //this u do search 
} 

function count_search($text){ 
    //This same logic with search but return Integer value for example 
    //return $this->db->query()->num_rows(); 
}