dqouryz3595 2017-04-05 04:55
浏览 41
已采纳

Codeigniter分页:错误自动增量编号

I have error pagination and just read this question:

Incrementing Operator in Foreach Loop for Pagination

Its working when my pagination is active. When my pagination is inactive, my web has bugs on auto increment number. Here's the picture:

Page when my pagination is inactive

Page when my pagination is active

Here's my model:

public function countGate()
  {
      return $this->db->count_all_results("tbl_gate");
  }

  public function paginationGate($limit, $start)
  {
      $this->db->select('*');
      $this->db->from('tbl_gate');
      $this->db->limit($limit, $start);
      return $this->db->get();
  }

Here's my controller:

public function gate()
  {
      $this->load->view('template/header');
      $this->load->view('admin/admin-header');

      $config = array();
      $config["base_url"] = base_url() . "index.php/admin/gate";
      $config["total_rows"] = $this->pagination_m->countGate();
      $config["per_page"] = 8;
      $config["uri_segment"] = 3;

      $this->pagination->initialize($config);
      $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
      $data['allgate'] = $this->pagination_m->paginationGate($config["per_page"], $page);
      $data['links'] = $this->pagination->create_links();

      $this->load->view('admin/admin-gate', $data);
      $this->load->view('template/footer');
  }

Here's my view (table with autoincrement minus bug):

<table class="table table-bordered">
        <tr>
          <th>#</th>
          <th>Fullname</th>
          <th>Gate</th>
          <th>Password</th>
          <th>Access</th>
          <th>Active</th>
          <th colspan="2">Action</th>
        </tr>
        <?php
          $i = 1 + ($this->pagination->cur_page-1)*$this->pagination->per_page;
          foreach ($allgate->result() as $gate){
        ?>
        <tr>
          <td><?= $i++ ?></td>
          <td><?= $gate->Fullname ?></td>
          <td><?= $gate->Username ?></td>
          <td><?= $gate->Password ?></td>
          <td><?= $gate->Level ?></td>
          <td><?= $gate->Active ?></td>
          <td><a href="<?= site_url('AdminUpdate/updateGate/'.$gate->IdGate)?>" data-toggle="modal" data-target="#update-gate">Edit</a></td>
          <td><a href="<?= site_url('admindelete/deletegate/'.$gate->IdGate)?>">Delete</a></td>
        </tr>
        <?php } ?>
      </table>
  • 写回答

1条回答 默认 最新

  • duanjian4331 2017-04-05 05:15
    关注

    It will work. I am using same pagination. Update controller:

    public function gate()
    {
        $this->load->view('template/header');
        $this->load->view('admin/admin-header');
    
        $config = array();
    
        $config["uri_segment"] = 3;
        $config["base_url"] = base_url("index.php/admin/gate");
        $config["per_page"] = 8;
    
        //add this also
        $config['use_page_numbers'] = TRUE;
        $config['first_url'] = '1';
    
        $config["total_rows"] = $this->pagination_m->countGate();
    
        $this->pagination->initialize($config);
    
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $offset = ($page == 0 ? 0 : ($page - 1) * $config["per_page"]);
    
        $data['allgate'] = $this->pagination_m->paginationGate($config["per_page"], $offset)->result();
        $data['links'] = $this->pagination->create_links();
    
        $data['start'] = ($page == 0 ? 1 : (($page - 1) * $config["per_page"] + 1));
    
        $this->load->view('admin/admin-gate', $data);
        $this->load->view('template/footer');
    }
    

    Updated View:

    <table class="table table-bordered">
        <tr>
            <th>#</th>
            <th>Fullname</th>
            <th>Gate</th>
            <th>Password</th>
            <th>Access</th>
            <th>Active</th>
            <th colspan="2">Action</th>
        </tr>
            <?php foreach ($allgate as $gate){ ?>
            <tr>
                <td><?= $start; ?></td>
                <td><?= $gate->Fullname ?></td>
                <td><?= $gate->Username ?></td>
                <td><?= $gate->Password ?></td>
                <td><?= $gate->Level ?></td>
                <td><?= $gate->Active ?></td>
                <td><a href="<?= site_url('AdminUpdate/updateGate/'.$gate->IdGate)?>" data-toggle="modal" data-target="#update-gate">Edit</a></td>
                <td><a href="<?= site_url('admindelete/deletegate/'.$gate->IdGate)?>">Delete</a></td>
            </tr>
            <?php 
                $start++;
            } ?>
    </table>
    <ul class="pagination">
        <?php echo $data['links']; ?>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?