Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get data in descending order when using pagination(using codeigniter )?

i have use codeigniter library function for pagination. it works but i didn't get how to display the content in descending order.

public function news($offset = 0) {
    $this->load->library('pagination');
    $config['base_url'] = base_url() . "index.php/homepage/inbox_news";
    $config['total_rows'] = $this->db->count_all_results('news');;
    $config['per_page'] = 3;
    $config['full_tag_open'] = '<div class="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);
    $data['posts'] = $this->db->limit(3, $offset)->get('news')->result();
    $this->load->view('inbox_news', $data);
}

//here news is my table name in database

in the view page, i have done like this

<?php
 foreach ($posts as $row) {
     echo $news = $row->news;
 }
echo $this->pagination->create_links();
?>

//it is working but the data is in ascending order by default. what can i do to put it in ascending order?

like image 401
Tsujit Pathak Avatar asked Mar 14 '26 19:03

Tsujit Pathak


1 Answers

try to add this before you get the news

$this->db->order_by("id", "desc");
$data['posts'] = $this->db->limit(3,$offset)->get('news')->result();
like image 79
salahy Avatar answered Mar 16 '26 09:03

salahy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!