我在我的搜索页面上实现 CI 分页时遇到问题,我的分页链接就像http://localhost/ci/search/2
(第 2 页)等等,但是我的查询可以在第一次加载时填充搜索结果,当我点击第 2 页开始时,它显示No items in database
,我尝试 echo$this->db->last_query()
在第 2 页检查我的查询,它显示SELECT * FROM ('products') WHERE MATCH (name, manufacturer) AGAINST ("") LIMIT 2, 3
缺少要搜索的关键字和 LIMIT 值不正确的地方,有人可以给我一个关于代码有什么问题的建议吗?
模型:
public function do_search_count($keywords)
{
$sql = "SELECT COUNT(*) AS cnt FROM products WHERE MATCH (name, manufacturer) AGAINST ('".$keywords."')";
$q = $this->db->query($sql);
$row = $q->row();
return $row->cnt;
}
public function do_search($keywords, $limit, $per_page)
{
$this->db->escape($keywords);
$this->db->where('MATCH (name, manufacturer) AGAINST ("'.$keywords.'") LIMIT '.$limit.', '.$per_page, NULL, FALSE);
$query = $this->db->get('products');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
public function searchterm_handler($searchterm)
{
if($searchterm)
{
$this->session->set_userdata('searchterm', $searchterm);
return $searchterm;
}
elseif($this->session->userdata('searchterm'))
{
$searchterm = $this->session->userdata('searchterm');
return $searchterm;
}
else
{
$searchterm = '';
return $searchterm;
}
}
控制器:
function search()
{
$keywords = $this->input->post('search');
$searchterm = $this->db_model->searchterm_handler($this->input->get_post('search', TRUE));
$limit = ($this->uri->segment(2) > 0) ? $this->uri->segment(2) : 0;
$config['base_url'] = base_url('search');
$config['total_rows'] = $this->db_model->do_search_count($searchterm);
$config['per_page'] = 3;
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$choice = $config['total_rows'] / $config['per_page'];
$config['num_links'] = round($choice);
$this->pagination->initialize($config);
$data['results'] = $this->db_model->do_search(trim($keywords), $limit, $config['per_page']);
$data['pagination'] = $this->pagination->create_links();
$data['searchterm'] = $searchterm;
$data['total'] = count($data['results']);
$data['title'] = 'Search';
$this->load->view('header', $data);
$this->load->view('search', $data);
$this->load->view('footer', $data);
}
看法:
<div class="page-header"><h2>Search Result(s): <?php echo $total ?></h2></div>
<div>
<?php
if(is_array($results)){
echo $this->db->last_query();
?>
<table width="40%" class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th>Price</th>
</tr>
</thead>
<?php
foreach($results as $item){
?>
<tr>
<td><?php echo $item->name ?></td>
<td><?php echo $item->manufacturer ?></td>
<td><?php echo $item->price ?></td>
</tr>
<?php
}
?>
</table>
<?php
}else{
echo 'No items in database! '.$this->db->last_query();;
}
?>
</div>
<div><?php echo $pagination ?></div>
位于头文件中的搜索表单视图:
<div class="header-search">
<?php
if($this->uri->segment(1) != 'search'){
$this->session->unset_userdata('searchterm');
$searchterm = '';
}
$form_attr = array('class' => 'form-wrapper cf');
echo form_open('search', $form_attr);
$search_attr = array('name'=>'search', 'id'=>'search', 'value'=>$searchterm, 'placeholder'=>'company, place, keywords etc...', 'required'=>'required');
echo form_input($search_attr);
$btn_attr = array('name' => 'btn_search', 'id' => 'btn_search', 'type' => 'submit', 'content' => 'search');
echo form_button($btn_attr);
echo form_close(); ?>
</div>
</div>
谢谢。