Codeigniter:如何对搜索结果进行分页

时间:2015-12-15 09:23:08

标签: codeigniter search pagination

当我搜索关键字时,它只是在数据库中向我显示所有内容。然后,如果我点击分页中的下一个。关键字丢失了。如果单击分页中的下一个按钮,如何维护搜索的关键字?

控制器:

public function info($offset=0)
{       
$filter = $this->input->get("filter");
    $limit = 5;
    $this->load->library('pagination');
    $filter = $this->input->get("filter");


    $this->db->where('is_valid','1');
    $this->db->like('requested_by',$filter);
    $search = $this->input->get("search");
      $page = $this->uri->segment(3);
    $config["base_url"] = "/ticketing/index.php/ticketing/info/";

    $config['total_rows'] = $this->db->count_all_results('db_ticketing.tr_ticket');
    $config['per_page'] = $limit;
//  $config['base_url'] = ("/ticketing/index.php/ticketing/info/");
    $config['use_page_numbers'] = false;
    $config['num_links'] = 4;


    $config['first_link'] = 'First';
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();



$data['ticket_list'] = $this->ticketing_mdl->get_all_ticket( $limit, $offset);



    $this->load->view('ticketing/header');
    $this->load->view('ticketing/left_menu');
    $this->load->view('ticketing/info',$data);
}

模型

function get_all_ticket($limit,$offset)
{
    if($this->input->get('search')){
     $match = $this->input->get('search');
        $sql = "SELECT * FROM db_ticketing.tr_ticket WHERE requested_by LIKE '%$match%' limit $limit offset  $offset";
        return $this->db->query($sql);

    }
}

查看:

<form method="get" action="">
                <tr>
                    <td>Search:</td> 
                    <td><input type="text" name="search" /></td>

                </tr>
                </form> 

2 个答案:

答案 0 :(得分:1)

试试这个

在控制器中

$this->load->library('pagination');

$count = $this->ticketing_mdl->count_all_ticket(); # get total count matched
//product pagination
$config['base_url'] = base_url() . '/ticketing/index.php/info/';
$config['total_rows'] = $count;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$limit = $config['per_page'];

//Bootstrap pagination style
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';


$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();

$data['ticket_list'] = $this->ticketing_mdl->get_all_ticket($limit,$page);

$this->load->view('ticketing/header');
$this->load->view('ticketing/left_menu');
$this->load->view('ticketing/info',$data);

**在模特**

# get count of record
function count_all_ticket()
{
    if($this->input->get('search')){
        $match = $this->input->get('search');
        $sql = "SELECT * FROM db_ticketing.tr_ticket WHERE requested_by LIKE '%$match%'";
        $query =  $this->db->query($sql);
        $result = $query->result_array();

        $count = count($result);
        return $count;

    }
}

# get all results matched
function get_all_ticket($limit,$page)
{
    if($this->input->get('search'))
    {
        $match = $this->input->get('search');
        $sql = "SELECT * FROM db_ticketing.tr_ticket WHERE requested_by LIKE '%$match%'  LIMIT $page, $limit";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }
}

答案 1 :(得分:1)

我遇到了同样的问题。 我已经用这种方式解决了:

  • 我将搜索关键字从搜索表单通过post方法传到functionOne()。
  • 我刚刚创建了一个包含关键字的会话,然后重定向到另一个 函数说functionTwo()。
  • 在新创建的函数iefunctionTwo()中,我从会话中获取了关键字并编写了CodeIgniter分页代码。
  • 在分页代码的'base_url'中,我使用了functionTwo()。