Page title from database |
Hi guys,
I am looking for the best way to get the page title and description for blog posts loaded dynamically. For the moment in my controller I have something like: $this->data['blog_result'] = $this->my_model->getBlogByID($blog_id); and in my model: public function getBlogByID($blog_id) { $this->db->select('*'); $this->db->from('cm_blog'); $this->db->where('blog_id', $blog_id); $query = $this->db->get(); return $query->result(); } I've tried several different approaches but cant seem to be able to make it work. Thank you in advance
@yannis,
All you have to do is simply set the returned dataset to a variable and reference the data field in the returned dataset array... $blog_details = $this->my_model->getBlogById($blog_id); $this->data['blog_title'] = $blog_details[0]['blog_title']; (ALSO: the above is assuming that you will only receive one record - I hate to assume) P.S. I would verify that a none empty dataset is returned in your model or else you might have issues down the road. Something like... (in your model) return ($query->num_rows() > 0) ? $query->result_array() : False; Then in your controller do a basic check to make sure it is not false.
If your only getting one record do not use select * use select title.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |