I'm new at CodeIgniter, but I want to ask about pagination and filtering. I did them and they works great aside from each other, but I have two pages with filters and pagination too. They gives me some errors. -When I click Filter, the url is like this: company/components/all/names_of_filters so the filters works at uri segment(4). =========================================================================But when I load pagination it shows me company/components/all/pagination so the filters crashes with pagination. I declared uri segment(5) for loaded the pagination but the same thing. The idea is that the filter is changing from database and it is an dynamic part. How can I declare it on $conf[base_url]=(company/components/all/...../) and put in those dashes an function that is concatenate an function that changes names filter?! --------------------------------------------------------------------------------------------------------- *Has any method or not? * Best :)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Put this file myhelper_helper.php in helpers folder
function configPagination($url,$total_rows,$segment,$per_page=10)
{
$CI = get_instance();
$CI->load->library('pagination');
$config['base_url'] = site_url($url);
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['uri_segment'] = $segment;
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_links'] = 3;
$config['next_tag_open'] = "<li>";
$config['next_tag_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tag_close'] = "</li>";
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
Example of pagination using session library... Controller: Users.php
public function __construct()
{
parent::__construct();
$this->per_page = 20; // How many records per page...
$this->load->helper('myhelper');
$this->load->library('session');
$this->load->model('users_model');
}
function list($start=0)
{
if(isset($_POST['filter_status']))
{
$this->session->set_userdata('filter_status',$this->input->post('filter_status'));
}
$data['users'] = $this->users_model->get_all($start,$this->per_page);
$total = $this->users_model->count_all();
$data['pages'] = configPagination('users/list',$total,5,$this->per_page);
$data['start'] = $start;
$this->load->view('users_view',$data);
}
And Finally in your model Users_model.php
function __construct()
{
parent::__construct();
$this->load->database();
}
function get_all($start,$limit) {
if($this->session->userdata('filter_status')!='')
{
$this->db->where('status',$this->session->userdata('filter_status'));
}
$query = $this->db->get('users', $limit, $start);
return $query;
function count_all()
{
if($this->session->userdata('filter_status')!='')
{
$this->db->where('status',$this->session->userdata('filter_status'));
}
$query = $this->db->get('users');
return $query->num_rows();
}
In your view, something like this
<form action="" method="post" id="filter_form">
<div class="form-group">
<label>Status: </label>
<div class="controls">
<?php $status = array(0=>"Inactive", 1=>"Inactive",-1=>"Banned");?>
<select id="status-select" name="filter_status" class="form-contro">
<option value="">All</option>
<?php foreach ($status as $key=>$value) {
$sel = ($key==$this->session->userdata('filter_status'))?'selected="selected"':'';
?>
<option value="<?php echo $key;?>" <?php echo $sel;?>><?php echo $value ?></option>
<?php } ?>
</select>
<input type="submit" Value="Filter" />
</div></form>
回答2:
Controller
$this->load->library('pagination');
$pagination_config['base_url'] = base_url('/webs/webs1/all/');
$pagination_config['total_rows'] = $this->model_m->count_all();
$pagination_config['per_page'] = 2;
$pagination_offset = $this->uri->segment(5);
$this->pagination->initialize($pagination_config);
$data['pagination_links'] = $this->pagination->create_links();
//end
//fetch categories from the database
$data['categories'] = $this->model_m->get_categories();
$categoryID = $this->uri->segment(4);
if($categoryID == null){
redirect('backend/subcategories/all/filterAll');
}
if(!isset($categoryID) || ( $categoryID == "filterAll" )) {
$data['subcategories'] = $this->model_m->get_all_subcategories();
} else {
$data['subcategories'] = $this->model_m->get_by_category($categoryID);
}
$data['selected_category'] = $categoryID;
$data['view'] = $this->load->view($view,$data,true);
$this->load->view($viewlayout,$data);
MODEL
public function get_by_category($id){
$query = $this->db->get_where('items', array('prod_id' => $id));
if ($query->num_rows() > 0) { return $query->result(); }
return false;
}
public function get_all_subcategories($catID = NULL,$limit = NULL ,$offset = NULL ) {
if(isset($catID) && is_int($catID)) {
if(isset($limit) && isset($offset)){
$result = $this->db->get_where('items',array('prod_id' => $catID),$limit,$offset);
return $result->result();
}
$result = $this->db->get_where('items',array('prod_id' => $catID));
return $result->result();
}
if(isset($limit) && isset($offset)){
$result = $this->db->get('items',$limit,$offset);
return $result->result();
}
$result = $this->db->get('items');
return $result->result();
}
public function count_all() {
return $this->db->get('items')->num_rows();
}