1

我在我的搜索页面上实现 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>

谢谢。

4

2 回答 2

1

您的代码似乎有几个问题:

首先,您的$keywords变量取自发布数据,一旦您被重定向到分页页面,发布数据就会被清除。您需要在 url 字符串中传递此值或将其保存到会话中。

其次,您将发布数据传递到您的模型searchterm_handler()方法中。如上所述,分页页面上没有要发送到方法的发布数据。

如果您不希望将数据作为参数发送,则需要为该参数设置默认值:

public function searchterm_handler($searchterm = null)
{

}

如果您不这样做,您将收到如下错误:

Warning: Missing argument 1 in call to searchterm_handler() in ...
于 2013-10-02T09:49:38.170 回答
1

这是因为您正在从 $_POST 数组中获取查询数据。当您使用链接导航到第二页时,不会再次发布搜索数据。理想情况下,您需要将搜索词保存到 cookie 中,并在没有发布任何内容时使用它。

您还可以使用函数 $this->uri->assoc_to_uri($_POST) 将搜索词存储在 URL 中并附加到基本 url,然后使用 $this->uri->uri_to_assoc(); 检索数据。

您还可以在http://www.thephpcode.com生成用于分页搜索的代码

于 2013-10-02T09:43:58.407 回答