Сведения о вопросе

Gentleman

18:09, 22nd July, 2020

Теги

Как настроить пагинацию (pagination) на сайте Codeigniter. Весь блок показывается на одной странице.

Просмотров: 509   Ответов: 1

Весь блок новостей выводится на одной странице. Но кнопки пагинации правильно выводятся. Найдите ошибку в моём коде.

Мой контроллер:

$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/news/index/';    

$config['total_rows'] = count($this->news_model->get_rows());

$config['per_page'] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';            
$config['prev_link'] = '&laquo;';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo;';
$config['next_tag_open'] = '<li>';
$config['next_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>';
$config["num_links"] = round( $config["total_rows"] / $config["per_page"] );

$config['news'] = $this->news_model->get_news($config['per_page'], $this->uri->segment(3));
$this->pagination->initialize($config);
$config['pages'] = $this->pagination->create_links();
$this->load->view('templates/news_view', $config);

 

В модели прописано следующее:

public function get_rows(){
        
        $this->db->select('*');
        $this->db->from('news');
        $query = $this->db->get();
        return $query->result_array();    
 }

public function get_news($num, $seg){
    $this->db->select('*');
    $this->db->from('news');
    $query = $this->db->get($num, $seg);
    return $query->result_array();    
} 

 

В view написано:

<?php foreach ($news as $value){  ?>

<span> <?= $value['news'] ?> </span>

<?php } ?>

<div class="center">
       <?php if(isset($pages)) echo $pages; ?>
 </div> 



  Сведения об ответе

prince

18:35, 22nd July, 2020

Измените функцию get_news в вашей модели на:

public function get_news($num, $seg){
        $this->db->limit($num, $seg);
        $this->db->select('*');
        $this->db->from('news');
        $query = $this->db->get();
        return $query->result_array();    

}


Ответить на вопрос

Чтобы ответить на вопрос вам нужно войти в систему или зарегистрироваться