我对搜索结果的分页无效。它显示结果,但分页使其随机。请帮我。搜索正在运行,但不能在其中应用分页,它变得越来越复杂
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 :(得分:0)
您好在此链接中未检查搜索条件
$config['total_rows'] = $this->db->count_all_results('tbl_products');
将此行更改为
$config['total_rows'] = $this->db->count_all_results($search_term);
并编写正确的查询
答案 1 :(得分: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();
}