2015-05-15 26 views
1

我做了codeigniter分页显示我的产品在图像。 但是第二页有什么不对, 它不可持续或不连续。错误的计算codeigniter分页

我设置per_page是:6, 我的总的数据是11。

在页面1分的结果是按id形式1确定,显示图像 - 6,但是当我点击第二页从3开始 - 8 。

这是模型

public function record_count() 
{ 
    return $this->db->count_all('mytable'); 
} 

public function fetch_jenislogam($limit, $start) 
{ 
    $start--; 
    if($start<0) 
    { 
    $start=0; 
    } 
    $this->db->limit($limit, $start); 
    $query = $this->db->get("tb_jenislogam"); 
    return $query->result_array(); 
} 

这是我的看法

<?php 
foreach($results as $data) { ?> 
<div class="col-lg-3 text-center"> 
<a href="<?php echo base_url();?>item/subitem/<?php echo $data->id; ?>"> <!-- to direct product subitem --> 
<img class="img-responsive" src='<?php $img = $data->pics; 
if ($img) { 
echo base_url().'uploads/origin/'.$data->pics; 
} else { 
echo base_url().'uploads/origin/def-img.png'; 
} 
?>'></a> 
<br> 
</div> 
<div> 
<?php echo $data->id;?></div> 
<?php 
} 
?> 

和这个控制器的样子。

public function listitem() 
    { 
    $config = array(); 
    $config['base_url'] = base_url().'item/listitem'; 
    $config['total_rows'] = $this->item_m->record_count(); 
    $config['per_page'] = 6; 
    $config['uri_segment'] = 3; 
    $config['num_links'] = 2; 
    $config['use_page_numbers'] = TRUE; 
    $config['cur_tag_open'] = '<a class="current">'; 
    $config['cur_tag_close'] = '</a>'; 
    $config['next_link'] = '>'; 
    $config['prev_link'] = '<'; 

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


    $this->load->view('user_header'); 
    $this->load->view('user/usr_item_view', $data); 
    $this->load->view('user_footer'); 
} 

回答

0

你需要改变你的第二个参数的控制,意味着$page变量值:

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
$pageLimit = ($page*$config['per_page']); //Add this line 

之后通过$pageLimit变量

$data['results'] = $this->item_m->fetch_jenislogam($config['per_page'], $pageLimit); 

原因:limit功能的第二个参数被抵消。并在你的情况下,它是设置1,2,3 ...

+0

感谢它的工作只在第一次当我点击菜单'Item'显示项目1-6,但当我点击第二页,它得到了错误严重性:警告 消息:为foreach提供的参数无效@)@fatalerror –

+0

我已更改模型代码以清除错误。但仍有错误的内容放置。当我第一次从标题链接菜单'base_url?>控制器/方法'加载1 - 4内容访问页面,但是当我点击数字2时,它显示9到11的内容。 echo result result echo $ this-> db-> last_query(); exit();' SELECT * FROM'mytable'LIMIT 4' //当页面第一次加载时,以及当我点击第二页时SELECT *从'mytable' LIMIT 7,4'我不能点击第一个链接。 @fatalerror –

+0

我从这个线程找到了解决方案。 [链接](http://stackoverflow.com/questions/9046149/pagination-do-not-correct-display-page-numbers-codeigniter)。只需添加此代码 '$ start = $ per_page *($ start_from-1);' –