进行正确的分页

时间:2019-01-18 07:45:02

标签: php pagination config codeigniter-3

当我显示此页面的数据表时,它可以工作,但是当我尝试添加分页时,它将变成错误。即使我有类似的问题,在哪里也可以找到解决方案

如何制作正确的脚本以显示基于分页的表数据?

我的代码中的错误在哪里?

查看

<table class="table mt-3">
        <thead class="table-danger text-center">
            <tr>
                <th scope="col">Kode</th>
                <th scope="col">Type</th>
                <th scope="col">Nama Barang</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody class="text-center table-primary">

            <?php foreach ( $barang as $tb ) : ?>
                <tr>
                    <td class="text-uppercase"><a href="" class="text-dark text-decoration-none"><?= $tb['barang_kode'] ?></a></td>
                    <td class="text-capitalize"><a href="" class="text-dark text-decoration-none"><?= $tb['type_nama'] ?></a></td>
                    <td class="text-capitalize"><a href="" class="text-dark text-decoration-none"><?= $tb['barang_nama'] ?></a></td>
                    <td>
                        <button id="btnGroupDrop1" type="button" class="btn  btn-outline-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button>
                        <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                          <a class="dropdown-item" href="<?= base_url(); ?>gudang/hapus_type/<?= $tb['barang_id']; ?>" onclick="return confirm('Yakin akan menghapus data ini ?');">Hapus</a>
                          <a class="dropdown-item" href="<?= base_url(); ?>gudang/ubah_type/<?= $tb['barang_id']; ?>">Ubah</a>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="container">
          <?php  // Tampilkan link-link paginationnya
          echo $barang['pagination'];  ?>
    </div>

模型

$config['base_url'] = base_url('gudang/lihat_type');
        $config['total_rows'] = $this->db->query($query)->num_rows();
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $config['num_links'] = 3;

        $config['full_tag_open']   = '<ul class="pagination pagination-sm m-t-xs m-b-xs">';
        $config['full_tag_close']  = '</ul>';
        $config['first_link']      = 'First';
        $config['first_tag_open']  = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link']       = 'Last';
        $config['last_tag_open']   = '<li>';
        $config['last_tag_close']  = '</li>';
        $config['next_link']       = ' <i class="glyphicon glyphicon-menu-right"></i> ';
        $config['next_tag_open']   = '<li>';
        $config['next_tag_close']  = '</li>';
        $config['prev_link']       = ' <i class="glyphicon glyphicon-menu-left"></i> ';
        $config['prev_tag_open']   = '<li>';
        $config['prev_tag_close']  = '</li>';
        $config['cur_tag_open']    = '<li class="active"><a href="#">';
        $config['cur_tag_close']   = '</a></li>';
        $config['num_tag_open']    = '<li>';
        $config['num_tag_close']   = '</li>';        // End style pagination 
        $this->pagination->initialize($config); // Set konfigurasi paginationnya
        $page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
        $query .= " LIMIT ".$page.", ".$config['per_page'];
        $data['limit'] = $config['per_page'];
        $data['total_rows'] = $config['total_rows'];
        $data['pagination'] = $this->pagination->create_links(); 
        $data['barang'] = $this->db->query($query)->result_array(); 
        return $data

1 个答案:

答案 0 :(得分:0)

模型上,更改:

$data['pagination'] = $this->pagination->create_links(); 

$data['barang']['pagination'] = $this->pagination->create_links(); 

以使分页数据在$barang数组内移动。