CodeIgniter Forums
How to get Latest Topic for each category in Forum Home Page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to get Latest Topic for each category in Forum Home Page (/showthread.php?tid=59128)



How to get Latest Topic for each category in Forum Home Page - El Forum - 08-28-2013

[eluser]Unknown[/eluser]
I am creating a simple forum using the CodeIgniter.

I want to get latest topic for each category in Forum Home Page.

It's OK for a category page to get latest topic, but I can't get for Home Page.

What i want is in Attachment Photo.


My Controller for Home Page Category List -
Code:
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 -
Code:
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



How to get Latest Topic for each category in Forum Home Page - El Forum - 08-28-2013

[eluser]sv3tli0[/eluser]
Try this...
It must return latest posts, 1 per cat
Code:
function get_latest_posts()
{
  $this->db->from('topics')->order_by('topic_id','DESC')->group_by('topic_cat_id');
  $get_categories = $this->db->get();
  return $get_categories->result_array();
}



How to get Latest Topic for each category in Forum Home Page - El Forum - 08-28-2013

[eluser]CroNiX[/eluser]
"latest" topic...are you using a date field anywhere to determine that or are you assuming the highest ID's are the most recent?


How to get Latest Topic for each category in Forum Home Page - El Forum - 08-28-2013

[eluser]Unknown[/eluser]
by ID.