【问题标题】:Wrong calculation codeigniter pagination错误的计算codeigniter分页
【发布时间】:2015-07-27 09:19:00
【问题描述】:

我已经进行了 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');
 }

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    您需要更改控制器中的第二个参数,即$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 ...

    【讨论】:

    • 感谢它仅在我第一次单击菜单时起作用 &lt;a href="&lt;?php echo base_url(); ?&gt;item/listitem"&gt;Item&lt;/a&gt; 显示项目 1 - 6,但是当我单击第二页时,出现错误 Severity: Warning Message: Invalid argument supplied for foreach()@fatalerror
    • 我已更改模型代码以清除错误。但是,内容放置仍然错误。当我第一次从标题链接菜单base_url?&gt;controller/method 访问该页面时,加载 1 - 4 个内容,但是当我点击数字 2 时,它显示了 9 到 11 个内容。 echo result echo $this-&gt;db-&gt;last_query();exit(); SELECT * FROM 'mytable' LIMIT 4` //第一次加载页面时,当我点击第二个页面时 SELECT * FROM mytable` LIMIT 7, 4 `我不能点击第一个链接. @fatalerror
    • 我从这个线程中找到了解决方案。 link。只需添加此代码$start = $per_page * ($start_from-1);
    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多