我第一次使用Codeigniter
分页库。现在,当用户从搜索表单中搜索任何内容时,我想将URL变量传递到URL末尾的分页,如此http://localhost/sitename/brand/campaigns/6/?search="hello"
,以便在第二页上维护搜索结果。
知道如何处理codeigniter吗?
这是我的代码:
<form name="frmSearch" id="frmSearch" method="get" action="<?php echo base_url(); ?>brand/campaigns">
<input type="text" name="search" id="search" placeholder="Search campaigns" onchange="return trim(this)">
<button type="submit" class="search-submit"></button>
</form>
public function __construct(){
parent::__construct();
$this->load->model('Campaigns_list_model');
$this->load->library('pagination');
}
public function index($offset=0){
IsNotLoggedIn(); // Check if a user is not logged in
// For SEO
$data['meta_title'] = 'Search';
$data['meta_descripton'] = '';
$data['meta_keywords'] = '';
// Pagination code for campaigns code start
$config['total_rows'] = $this->Campaigns_list_model->TotalCampaigns();
// Check status value is number
$status = $this->uri->segment(3);
if(!ctype_digit($status)){
$status = "1";
}
$config['base_url'] = base_url()."brand/campaigns/".$status;
$config['per_page'] = 6;
$config['uri_segment'] = '4';
$config['full_tag_open'] = '<div class=""><ul class="pagination pull-right">';
$config['full_tag_close'] = '</ul></div>';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$query = $this->Campaigns_list_model->GetCampaigns(6, $this->uri->segment(4));
$data['Campaigns'] = null;
if($query){
$data['Campaigns'] = $query;
}
// Pagination code for campaigns code end
$this->load->view('include/brand_header', $data);
$this->load->view('brand/campaigns_list', $data);
$this->load->view('include/brand_footer');
}
答案 0 :(得分:1)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My_controller extends CI_Controller {
public function __construct() {
parent::__construct();
//here we will autoload the pagination library
$this->load->library('pagination');
}
public function paginationExample()
{
//echo $this->uri->segment(2);die;
$config = array();
$config["base_url"] = base_url('My_controller/paginationExample');
$config['total_rows'] = $this->db->count_all("Your table");//here we will count all the data from the table
$config['per_page'] = 5;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["allData"] = $this->My_model->paginationExample($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$this->load->view('your view',$data);
}
}
有关如何在codeigniter中进行分页的尝试,请尝试以下方法:get
答案 1 :(得分:0)
您是否需要在网址中包含搜索标记?我的意思是,您可以将搜索标记保存在会话var上,也许可以使用它。这是我通常根据情况做的事情。这应该是,只要您有办法让用户重置他的搜索条件。
如果您确实需要像这样使用它,您可以这样做:
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
取自以下问题(你也应该检查一下): codeigniter pagination url with get parameters