CodeIgniter Forums

Full Version: Stuck creating a forum
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Procode[/eluser]
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:

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

Controller:
Code:
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>';
  
   $this->pagination->initialize($config);
  
   $data = array(
    'tmp' => 'talk/main',
    'posts' => $this->talk_model->get_threads($config['per_page'],$this->uri->segment(2))
   );
  
   $this->load->view('tmp', $data);
  }

View:
Code:
<tbody>
      &lt;?php foreach($posts as $p): ?&gt;
    <tr>
      <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>
    </tr>
    &lt;?php endforeach; ?&gt;
  </tbody>

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!

El Forum

[eluser]GrahamDj28[/eluser]
Use this query

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