当前位置: 首页 > 知识库问答 >
问题:

CodeIgniter搜索结果分页

徐高韵
2023-03-14

我到处找了又找,但还是找不到解决问题的办法。我还不熟悉php和codeigniter,所以可能我已经错过了答案,但不管怎样,下面是我要做的。

这是我的控制器(c_index.php)-调用搜索函数并对结果数组执行分页。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_index extends CI_Controller {


public function __construct()
{
    parent::__construct();
    $this->load->model('m_search');
    $this->load->library("pagination");
    $this->load->library("table");
}
/** Index Page for this controller */
public function index()
{
    if($this->session->userdata('logged_in')){
        $this->load->view('v_user');    
    }   
    else{
        $this->load->view('index'); 
    }
}

public function search() 
{
            // start of search
    $search_term = $this->input->post('word');
    $books = $this->m_search->search_books($search_term);

            // start of pagination
    $config['base_url'] = "http://localhost/test/index.php/c_index/search";
    $config['per_page'] = 5;
    $config['num_links'] = 7;
    $config['total_rows'] = count($books);

    echo $config['total_rows'];
    $this->pagination->initialize($config);
    $data['query'] = array_slice($books,$this->uri->segment(3),$config['per_page']);
    $this->load->view("index",$data);

}

}

这是我的视图(index.php)-基本上只是显示分页结果

<h3> Search Results </h3>   
    <!-- table -->              
    <table class="table table-condensed table-hover table-striped" id="result_table">
        <tbody>
        <?php
        if(isset ($query)){
            if(count($query)!=0){
                foreach($query as $item){
                echo "<tr><td>". $item['title'] ." by " .$item['author'] ."<br/></td></tr>";
                }
                echo $this->pagination->create_links();
            }
            else
            {
                echo "No results found for keyword ' ". $this->input->post('word')." ' .";
            }
        }
        else
        {
            echo "Start a search by typing on the Search Bar";
        }
        ?>              
        </tbody>
    </table>

我的模型(m_search.php)-基本上搜索数据库并返回结果数组。

<?php

类M_搜索扩展了CI_模型{

function search_books($search_term='default')
{

    $filter = $this->input->post('filter');
    //echo $filter;

    if($filter == 'title')
    {
        //echo 'title';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('title',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'author')
    {
        //echo 'author';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('author',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'type')
    {
        //echo 'type';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_type',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'status')
    {
        //echo 'status';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_status',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();
    }else
    {
        //echo 'all';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_status',$search_term);
        $this->db->or_like('book_type',$search_term);
        $this->db->or_like('author',$search_term);
        $this->db->or_like('title',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();
    }

}

}

现在我的问题是保留分页的结果。

第一个页面很好,但每当我单击页面链接时,表上的结果都会显示整个数据库,并且不限于我的搜索结果。

我在某个地方读到过,我需要使用会话来保持我的search_term,这样当我切换页面时它就可以工作,但我不知道把它放在哪里。任何意见或建议将不胜感激。谢谢。

共有3个答案

闻人修平
2023-03-14

您可以在控制器中使用以下代码,所有查询字符串都将显示在分页链接中。

$config['reuse_query_string']=TRUE;

对于分页链接中的其他参数,可以在下面两行中取消注释。

//$getData = array('s'=>$search);

//$config['suffix'] = '?'.http_build_query($getData,'',"&amp;");

$this->pagination->initialize($config);
师冥夜
2023-03-14
function search_books($search_term='default')
{

 $filter = $this->input->post('filter');
//echo $filter;
if($filter == 'title')
{
    $this->db->like('title',$search_term);
}
 else if($filter == 'author')
{
    $this->db->like('author',$search_term);
}
 else if($filter == 'type')
{
    $this->db->like('book_type',$search_term);

}
else if($filter == 'status')
{
    $this->db->like('book_status',$search_term);
}
 else
{

    $this->db->like('book_status',$search_term);
    $this->db->or_like('book_type',$search_term);
    $this->db->or_like('author',$search_term);
    $this->db->or_like('title',$search_term);
     // Execute the query.

    }
    $query = $this->db->get('book');
    return $query->result_array();

} 

}

只是为了减少模型代码,如果您不想使用“会话”并希望复制、粘贴链接/url并在搜索中获得相同的结果,那么您应该在控制器中使用uri类。

干永丰
2023-03-14

根据您的需要,有几种不同的方法来处理搜索和分页。基于你现有的代码,这就是我要做的。

改变

$search_term = $this->input->post('word');

$search_term = ''; // default when no term in session or POST
if ($this->input->post('word'))
{
    // use the term from POST and set it to session
    $search_term = $this->input->post('word');
    $this->session->set_userdata('search_term', $search_term);
}
elseif ($this->session->userdata('search_term'))
{
    // if term is not in POST use existing term from session
    $search_term = $this->session->userdata('search_term');
}
 类似资料:
  • 我目前正在参与使用Liferay(6.1 GA2)的项目。Liferay搜索结果似乎提供了指向Web内容片段的链接,而不是指向包含这些片段的页面的链接。 你们中有人经历过这个问题吗?你知道怎么解决吗? 非常感谢朋友们。 最好的,阿尔贝托

  • 我到处寻找解决方案,尝试了不同的解决方案,包括Chip Bennett在这里详细解释的解决方案,但我似乎仍然无法让它工作。 结果的第一页工作正常,但是从第2页开始,它只显示索引模板,并且仍然显示页面未找到。这是我的代码: 功能。php 提取查询参数的代码 循环代码: 任何建议将不胜感激。 编辑: 我还注意到,当我尝试访问第2页时,URL被修改了。 这是第1页URL: 第2页URL:

  • 我有这个搜索。按年份/类型显示搜索结果的php: 的设置 在wordpress是显示200结果。我希望这些结果被分页,假设每页20篇文章,然后下一篇,下一篇...在页面内,没有其他结果页面的链接。 我试图添加 ,这是我从Category页面获得的一段代码,在该页面中分页工作没有问题,但在搜索结果中不显示任何与分页相关的内容。类别页面代码为: 而且该类别的导航效果很好,我如何在搜索页面的结果中获得相

  • 我们在Liferay DXP和Elasticsearch 2.2.0中有一个自定义搜索portlet。我们在elasticsearch设置中为同义词搜索添加了以下设置。 我们还使用以下代码验证是否将同义词分析器添加到索引中。 这给出了synonyms.txt文件中“acl”的所有同义词的结果。但是Liferay搜索不会给搜索匹配同义词。例如:搜索(“acl”)=

  • 我使用的是spring数据elasticsearch(4.0版)。现在,我需要将rest高级客户端的搜索结果转换为POJO对象。 我会使用杰克逊图书馆的ObjectMapper。我相信有更好的方法来做到这一点。 Spring data elasticsearch now(4.0版)使用MappingElasticsearchConverter。不幸的是,我不知道如何做到这一点-我没有看到任何相关文

  • 我正在用Hibernate Search 4.5.1和Spring 4.0.5版本构建一个应用程序。我正在尝试索引以下类: 我正在构建一个junit测试用例,看起来如下所示: 我还注意到在luke lucene上,一些索引词的长度最多为6个字符,例如,一首歌的艺术家是“后代”,而索引中存储的词是“the”和“offspr”。第一个可以,但第二个不应该是“后代”。为什么要截断名字?