CodeIgniter 搜索结果分页

Posted

技术标签:

【中文标题】CodeIgniter 搜索结果分页【英文标题】:CodeIgniter Search Results Pagination 【发布时间】:2014-03-26 18:20:18 【问题描述】:

好吧,我已经四处搜索,但仍然找不到解决问题的方法。我还是 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_search 类扩展 CI_Model

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,以便在我切换页面时它可以工作,但我不知道该放在哪里。 任何意见或建议将不胜感激。谢谢。

【问题讨论】:

我使用 ajax + jQuery 数据表。对我来说似乎更容易,也许它对你有用? [链接]datatables.net 【参考方案1】:

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

改变

$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');

【讨论】:

谢谢。我会尝试一下。希望这能解决我的问题^^ @user3348358 是否解决了您遇到的问题【参考方案2】:
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 类。

【讨论】:

【参考方案3】:

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

$config['reuse_query_string']=TRUE;

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

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

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

$this->pagination->initialize($config);

【讨论】:

【参考方案4】:

在你的Controller中尝试这种方式,

    public function search()
       
    $result=array();
    if($this->session->userdata('login_id')!='')
    
        $q = trim($this->input->get('q'));
        if(strlen($q)>0)
        
            $config['enable_query_strings']=TRUE;
            $getData = array('q'=>$q);
            $config['base_url'] = base_url().'admin/Product/search/';   
            $config['suffix'] = '?'.http_build_query($getData,'',"&amp;");
            $config['first_url'] = $config['base_url'].'?q='.$q;
            $config["per_page"] = 10;
            $config['use_page_numbers'] = TRUE;
            $total_row = $this->Prod_model->search_record_count($q);
            $config['total_rows']=$total_row;
                $config['num_links'] = $total_row;
                $config['cur_tag_open'] = '&nbsp;<a class="current">';
                $config['cur_tag_close'] = '</a>';
                $config['next_link'] = 'Next >';
                $config['prev_link'] = '< Previous';
            if($this->uri->segment(3))
            
                $page = $this->uri->segment(3);
            
            else
            
                $page = 1;
            
            $result['search'] = $this->Prod_model->search($q,$config["per_page"],$page);
            if(empty($result['search']))
            
                echo "no data found";die;           
            
            else
            
                $this->pagination->initialize($config); 
                    $str_links = $this->pagination->create_links();
                    $result["links"] = explode('&nbsp;',$str_links);
                        $id=$this->session->userdata('login_id');
                        $result['user']=$this->Home_model->user_details($id);
                        $this->template->load('prod_table',$result);                
            
        
        else
        
            echo "empty string";die;
        
    
    else
    
        redirect(base_url()."admin/");
    
   

【讨论】:

以上是关于CodeIgniter 搜索结果分页的主要内容,如果未能解决你的问题,请参考以下文章

如何在codeigniter中执行排序、搜索、分页

Codeigniter 分页显示结果数和页数

codeigniter 分页 - 结果在每一页上保持不变

在 codeigniter 上搜索和分页

CodeIgniter 中的分页问题

如何使用 codeigniter 分页库回显页码?