php - Get Latest Topic for each category in Forum Home Page in CodeIgniter -
i creating simple forum using codeigniter.
i want latest topic each category in forum home page. want so-
it's ok category page latest topic, can't home page
my controller home page category list -
class category extends ci_controller { public function index() { $this->load->model('category_model'); $data['categories'] = $this->category_model->get_all_categories(); $this->load->view('forums/index', $data); } }
my model -
class category_model extends ci_model { function get_all_categories() { $get_categories = $this->db->get('categories'); return $get_categories->result_array(); } }
db structure-
topics
- topic_id
- topic_title
- topic_content
- topic_cat_id
categories
- cat_id
- cat_name
- cat_description
you can try this, hope helps:
function get_all_categories() { $data = array(); $get_categories = $this->db->get('categories'); $cat = $get_categories->result_array(); foreach( $cat $key=>$each ){ $rs = $this->db->where('topic_cat_id', $each['cat_id'])->oreder_by('topic_id', 'desc')->get('topics', 1)->row_array(); $data[$key]['cat'] = $each; $data[$key]['top'] = $rs; } echo "<pre>";print_r( $data ); return $data; }
Comments
Post a Comment