Welcome Guest, Not a member yet? Register   Sign In
Stuck creating a forum

Hello, I am still learning how to fully use codeigniter and I am sort of stuck. I am trying to create a small forum and I have my model, controller, and view set-up where it works fine and grabs all the threads from the table. However what I don't know how to do it how can I also grab the user information for each thread.

Here is my code:

function get_threads($num, $offset)
  $this->db->order_by('date', 'desc');
  $query = $this->db->get('talk', $num, $offset);
  return $query->result();

public function main()
      $config['base_url'] = base_url().'talk/';
      $config['total_rows'] = $this->db->count_all('talk');
      $config['per_page'] = '1';
      $config['full_tag_open'] = '<p>';
      $config['full_tag_close'] = '</p>';
   $data = array(
    'tmp' => 'talk/main',
    'posts' => $this->talk_model->get_threads($config['per_page'],$this->uri->segment(2))
   $this->load->view('tmp', $data);

      &lt;?php foreach($posts as $p): ?&gt;
      <td><i class="icon-comment"></i><a href="topic.php?id=&lt;?php echo $p-&gt;id; ?&gt;"> &lt;?php echo $p->title; ?&gt;</a></td>
      <td><i class="icon-user"></i> Username</td>
      <td><i class="icon-time"></i> &lt;?php echo date('D, M j', $p->date); ?&gt;</td>
    &lt;?php endforeach; ?&gt;

How can I also grab the user information for each thread and display the username? I do have the owner_id in the table for the threads and all, I'm just not sure how to use it exactly in order to also pass the results to my view where each thread would get their respected owners information. I was thinking of doing a foreach in the controller and then grabbing the user info, however I am confused of how I would pass all the information to the view and then loop through everything again?

Thank you for your help in advance!

Use this query

SELECT t.*, user.username
FROM talk AS t
LEFT JOIN users AS user ON user.id = t.owner_id

Theme © iAndrew 2016 - Forum software by © MyBB