passing multiple queries to view with codeigniter
- by LvS
I am trying to build a forum with Codeigniter.
So far i have the forums themselves displayed and the threads displayed, based on the creating dynamic news tutorial.
But that is 2 different pages, i need to obviously display them into one page, like this:
Forum 1
- thread 1
- thread 2
- thread 3 
Forum 2
- thread 1
- thread 2 
etc.
And then the next step is obviously to display all the posts in a thread. Most likely with some pagination going on. But that is for later.
For now i have the forum controller (slimmed version):
<?php
class Forum extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('forum_model');
        $this->lang->load('forum');
        $this->lang->load('dutch');
    }
    public function index()
    {
        $data['forums'] = $this->forum_model->get_forums();
        $data['title'] = $this->lang->line('title');
        $data['view'] = $this->lang->line('view');
        $this->load->view('templates/header', $data);
        $this->load->view('forum/index', $data);
        $this->load->view('templates/footer');
    }
    public function view($slug)
    {
        $data['forum_item'] = $this->forum_model->get_forums($slug);
        if (empty($data['forum_item']))
        {
            show_404();
        }
        $data['title'] = $data['forum_item']['title'];
        $this->load->view('templates/header', $data);
        $this->load->view('forum/view', $data);
        $this->load->view('templates/footer');
    }
}
?>
And the forum_model (also slimmed down)
<?php
class Forum_model extends CI_Model {
    public function __construct()
    {
        $this->load->database();
    }
    public function get_forums($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query= $this->db->get('forum');
            return $query->result_array();
        }
        $query = $this->db->get_where('forum', array('slug' => $slug));
        return $query->row_array();
    }
        public function get_threads($forumid, $limit, $offset)
    {
            $query = $this->db->get_where('thread', array('forumid', $forumid), $limit, $offset);
            return $query->result_array();
    }
}
?>
And the view file
<?php foreach ($forums as $forum_item): ?>
    <h2><?=$forum_item['title']?></h2>
    <div id="main">
        <?=$forum_item['description']?>
    </div>
    <p><a href="forum/<?php echo $forum_item['slug'] ?>"><?=$view?></a></p>
<?php endforeach ?>
Now that last one, i would like to have something like this:
<?php foreach ($forums as $forum_item): ?>
    <h2><?=$forum_item['title']?></h2>
    <div id="main">
        <?=$forum_item['description']?>
    </div>
    <?php foreach ($threads as $thread_item): ?>
    <h2><?php echo $thread_item['title'] ?></h2>
    <p><a href="thread/<?php echo $thread_item['slug'] ?>"><?=$view?></a></p>
    <?php endforeach ?>
<?php endforeach ?>
But the question is, how do i get the model to return like a double query to the view, so that it contains both the forums and the threads within each forum.
I tried to make a foreach loop in the get_forum function, but when i do this:
public function get_forums($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query= $this->db->get('forum');
            foreach ($query->row_array() as $forum_item)
            {
                $thread_query=$this->get_threads($forum_item->forumid, 50, 0);
            }
            return $query->result_array();
        }
        $query = $this->db->get_where('forum', array('slug' => $slug));
        return $query->row_array();
    }
i get the error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/forum_model.php
Line Number: 16
I hope anyone has some good tips, thanks!
Lenny
*EDIT***
Thanks for the feedback.
I have been puzzling and this seems to work now :)
    $query= $this->db->get('forum');
        foreach ($query->result() as $forum_item)
        {
            $forum[$forum_item->forumid]['title']=$forum_item->title;
            $thread_query=$this->db->get_where('thread', array('forumid' => $forum_item->forumid), 20, 0);
            foreach ($thread_query->result() as $thread_item)
            {
                $forum[$forum_item->forumid]['thread'][]=$thread_item->title;
            }
        }
        return $forum;
    }
What is now next, is how to display this multidimensional array in the view, with foreach statements....
Any suggestions ?
Thanks