我在搜索页面中实现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>
感谢。
答案 0 :(得分:1)
这是因为您从$ _POST数组获取查询数据。使用链接导航到第二页时,搜索数据不会再次发布。理想情况下,您需要将搜索词保存到cookie中,并在未发布任何内容时使用它。
您还可以使用函数$ this-&gt; uri-&gt; assoc_to_uri($ _ POST)将搜索字词存储在网址中,并附加到基本网址,然后使用$ this-&gt; uri-检索数据&GT; uri_to_assoc();
处生成用于分页搜索的代码答案 1 :(得分:1)
您的代码似乎存在一些问题:
首先,您的$keywords
变量取自发布数据,一旦您被重定向到分页页面,就会清除发布数据。您需要在url字符串中传递此值或将其保存到会话中。
其次,您将发布的数据传递到模型searchterm_handler()
方法中。如上所述,在分页页面上没有要发送到方法的发布数据。
如果您希望不将数据作为参数发送,则需要为该参数设置默认值:
public function searchterm_handler($searchterm = null)
{
}
如果您不这样做,您将收到如下错误:
Warning: Missing argument 1 in call to searchterm_handler() in ...