Welcome Guest, Not a member yet? Register   Sign In
Don't know where to put code for passing multiple dataqueries to view
#1

[eluser]Unknown[/eluser]
Hi,

I have a problem with the figuring out how to put this in a model and then passing it onto the view. And then obviously how to display the data in the view....

I am trying to build a forum.
A forum has threads and threads have posts.

For the forum overview im trying to accomplish to display:

Forum 1
- thread 1
- thread 2
- thread 3
Forum 2
- thread 1
- thread 2
etc.

So far i have the forums displaying by themselves correctly and the threads by themselves.
Based on the news tutorial.

For the second step to display them together, i am trying to do this in the forum_model:

Code:
$forum_query= $this->db->get('forum');
  
   foreach ($forum_query->row_array() as $forum_item){
    echo $forum_item->title ."<br>";
   $thread_query= $this->db->query('SELECT * FROM thread where forumid='.$forum_item-&gt;forumid);
    foreach ($thread_query-&gt;row_array() as $thread_item){
    echo $thread_item-&gt;title ."&lt;br&gt;";
    }

   }

I know i am not supposed to echo within the model, but that is just for testing. Please also advice me how to pass the double database query on to the view and how to actually display it there.

I already encounter numerous errors, but i don't really see what i'm doing wrong.
Any advise?

Thanks!
#2

[eluser]Unknown[/eluser]
Ok, i have got PART 1, building the array in the model:

Code:
public function get_forums($slug = FALSE)
{
  if ($slug === FALSE)
  {
   $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;
  }
  
  $query = $this->db->get_where('forum', array('slug' => $slug));
  return $query->row_array();
}

Now im working on PART 2, displaying this array correctly with foreach statements, in the view.

Any suggestions??




Theme © iAndrew 2016 - Forum software by © MyBB