我做了分页,但它不能限制每页codeigniter的帖子

时间:2017-07-08 06:20:19

标签: php codeigniter

这是我的控制器,我使用$config['per_page']来限制我的帖子,但它仍无法正常显示我的数据库,

function index()

{

    $jumlah_data = $this->m_berita->jumlah_data();

    $config['base_url'] = base_url().'berita/index/';
    $config['total_rows'] = $jumlah_data;
    $config['per_page'] = 2;
    $from = $this->uri->segment(3);

    $this->pagination->initialize($config); 

    $halaman['berita'] = $this->m_berita->data($config['per_page'],$from);

    $urutan['berita'] = $this->db->order_by("id", "desc");

    $data['berita'] = $this->m_berita->data_berita()->result(); 

    $this->load->view('artikel', $data, $urutan, $halaman);     

}

我的模特

class m_berita extends CI_Model{

public function __construct()

    {
        $this->load->database();
    }

    public function data_berita(){
        return $this->db->get('berita');

    }

    public function data($number,$offset){
        return $query = $this->db->get('berita',$number,$offset)->result(); 
    }

    function jumlah_data(){
        return $this->db->get('berita')->num_rows();
    }

和我的观点

    <?php 
$no = $this->uri->segment('3') + 1;
foreach ($berita as $u) { 
?>

<?php $no++;?>
<div class="container">
<?php $u->id; ?> 
    <h4> <?php echo $u->judul; ?> </h4>
    <span style="margin-bottom: 5%;" class="image left 3u"><img class="fit" src="<?php echo base_url('images/').$u->gambar; ?>" alt="" /></span>
    <?php $artikel = $u->isi; $artikel=word_limiter($artikel, 98); ?> <?php echo $artikel;?> <?php echo anchor('berita/tampil_berita/'.$u->id,'Read More'); ?>
</div>

<?php } ?>
<div class="container">
<ul class="pagination">
  <li><?php echo $this->pagination->create_links(); ?></li>
</ul>
</div>

2 个答案:

答案 0 :(得分:1)

更改数据库功能

function jumlah_data(){
    return $this->db->count_all('berita');
}

答案 1 :(得分:1)

控制器代码:

$this->load->library('pagination');

$config['base_url'] = 'base_url().'berita/index/';

$config['total_rows'] = $this->db->get('berita')->num_rows();

$config['next_link'] = 'Next';

$config['prev_link'] = 'Previous';

$config['uri_segment'] = 4;

$config['per_page'] = 10;

$config['num_links'] = 20;

$config['full_tag_open'] = '<div id="pagination" class="pagination">';

$config['full_tag_close'] = '</div>';

$this->pagination->initialize($config);

$this->data['berita'] = $this->m_berita->data_berita($config['per_page']);

型号代码:

function data_berita($per_page) {

        $query = $this->db->get('berita', $per_page, $this->uri->segment(3));

        return $query->result_array();

    }

查看代码:

echo $this->pagination->create_links();

然后循环