ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-CodeIgniter搜索结果分页

2019-11-22 03:31:23  阅读:162  来源: 互联网

标签:search codeigniter pagination php


好吧,我已经四处搜寻,但是仍然找不到解决我问题的方法.我还是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,以便它在我切换页面时起作用,但我不知道将其放在哪里.
任何意见或建议,将不胜感激.谢谢.

解决方法:

根据您的需求,有几种不同的方式来处理搜索和分页.根据您现有的代码,这就是我要做的.

更改

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

标签:search,codeigniter,pagination,php
来源: https://codeday.me/bug/20191122/2056767.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有