Codeigniter分页显示带有页面查询字符串的重复记录 [英] Codeigniter pagination showing duplicate records with page query string
本文介绍了Codeigniter分页显示带有页面查询字符串的重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了与codeigniter 3分页。
在每个页面我得到相同的结果:
我的最终url看起来像: http:// localhost / shoping / products / mobile-phones?cid = 3& page = 2
i have created pagination with codeigniter 3. On every page i am getting same results: my final url seems like: http://localhost/shoping/products/mobile-phones?cid=3&page=2
控制器文件:
public function category(){
$category_id = $this->input->get('cid', TRUE);
$slug = $this->uri->segment(2);
$sortby = $this->input->get('sort', TRUE);
$sorttype= 'ASC';
$limit = 10;
$config = array();
$config['base_url'] = base_url() . 'products/'.$slug.'?cid='.$category_id;
$config['total_rows'] = $this->Product_model->category_product_count($category_id);
$config['per_page'] = $limit;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
// $config["uri_segment"] = 3;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['next_link'] = '»';
$config['cur_tag_open'] = '<li><a class="current">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
if($this->uri->segment(2) > 0){
$page = ($this->uri->segment(2) + 0)*$config['per_page'] - $config['per_page'];
}
else{
$page = $this->uri->segment(2);
}
$data["links"] = $this->pagination->create_links();
if($data["links"]!= '') {
$data['pagermessage'] = 'Showing '.((($this->pagination->cur_page-1)*$this->pagination->per_page)+1).' to '.($this->pagination->cur_page*$this->pagination->per_page).' of '.$config['total_rows'];
}
if($this->input->get('sort')=='pricelow'){
$sortby = 'price';
$sorttype= 'ASC';
}
elseif($this->input->get('sort')=='pricehigh'){
$sortby = 'price';
$sorttype= 'DESC';
}
elseif($this->input->get('sort')=='new'){
$sortby = 'updated_on';
$sorttype= 'DESC';
}
elseif($this->input->get('sort')=='popularity'){
$sortby = 'price';
$sorttype= 'DESC';
}
else{
$sortby = 'title';
$sorttype= 'ASC';
}
//Get all products
$data['products_by_category'] = $this->Product_model->get_products_by_category($category_id, $config["per_page"], $page, $sortby, $sorttype);
// echo $config['total_rows'];
// Load View
$data['main_content'] = 'public/products_category';
$this->load->view('public/layouts/home_main', $data);
}
模型文件:
public function get_products_by_category($category_id, $limit, $start='', $shortby='', $shorttype=''){
$this->db->select('*');
$this->db->where('category_id', $category_id);
$this->db->limit($limit, $start);
$this->db->order_by($shortby, $shorttype);
$query = $this->db->get('products');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
并查看文件:
<?php foreach($products_by_category as $product): ?>
<div class="col-sm-4 col-md-4 col-lg-3">
<div class="single-product" style="height:300px;margin-bottom:10px">
<span class="sale-on">sale</span>
<div class="product-image">
<div class="show-img">
<a href="<?php echo base_url();?><?php echo $product->slug.'?sid='.$product->id;?>">
<img src="<?php echo base_url(); ?>assets/images/products/<?php echo $product->image;?>" alt="<?php echo $product->title;?>" height="150" width="150">
</a>
</div>
</div>
<div class="prod-info">
<h2 class="pro-name">
<a href="<?php echo base_url();?><?php echo $product->slug.'?sid='.$product->id;?>"><?php echo $product->title.$product->id;?></a>
</h2><br>
<div class="price-box">
<div class="price">
<span><i class="fa fa-inr"></i><?php echo $product->price;?></span>
</div>
<div class="old-price">
<span><i class="fa fa-inr"></i><?php echo $product->mrp_price;?></span>
</div>
</div>
<div class="actions">
<span class="new-pro-wish">
<a href="#" data-toggle="tooltip" title="Add to wishlist"><i class="fa fa-heart-o"></i></a>
</span>
<span class="new-pro-compaire">
<a href="#" data-toggle="tooltip" title="Add to compare"><i class="fa fa-bar-chart"></i></a>
</span>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
推荐答案
> uri-> segment(2),请使用 $ this-> input-> get('page',TRUE)
。
Instead of $this->uri->segment(2)
, use $this->input->get('page', TRUE)
.
请参阅 CI_Input :: get 有关如何查询字符串变量的更多信息。
See CI_Input::get for more information on how you can get query string variables.
这篇关于Codeigniter分页显示带有页面查询字符串的重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文