Reputation: 1
I am building a blog in Codeigniter to get more familiar with it and to help my PHP and CI skills. on my main page where I am displaying posts I am trying to get the newest post to show first but my below code is not working. any thoughts? I have looked and found some info that I have added to the code but it still is not working. The below does not have any errors when I load the page but it is not ordering them either.
Thanks in advance
$data['posts'] = $this->db->order_by('post_id','DECS')->get('posts', $config['per_page'],$this->uri->segment(3));
Upvotes: 0
Views: 1101
Reputation: 472
It is not good practice to mixing codes together of MODEL and Controller of MVC framework like CodeIgniter. you should follow MVC pattern. So Try to keep code in controller as
$data['posts'] =$this->MODEL_NAME->METHOD_NAME(param1, param2,param2..);
Code your entire query in Model method as
function METHOD_NAME(param1, param2,param2...){
$this->db->select('table_name.*')->from('table_name');
$this->db->where( array('tareget_field'=>param1,'tareget_field'=>param2,'tareget_field2'=>param3));
// You may use limit to get selected rows
$this->db->limit(param..);
$this->db->order_by('post_id','desc'); // **Here is your solution**.
return $this->db->get()->result_array();
}
Hope it will work for you!
Thank you!
Upvotes: 1