codeigniter加入问题

时间:2010-11-12 22:34:33

标签: database codeigniter join

我在我的数据库“forum_traad”和“forum_kommentare”中有2个表格,但是他们有相同的行“indhold”所以当我尝试加入forum_traad和forum_kommentare时我想回应“forum_traad”中的“indhold”行它回声来自“forum_kommentare”的“indhold”,我该怎么办?

我的观点:

<div id="forum">


  <?php 
  if($query)
  {
  ?>
  <div class="forum_headline">Forum kategori - Forum tråde - <?php echo $query->overskrift; ?></div><!-- forum_headline -->
  <div class="forum_profil_img"></div><!-- forum_profil_img -->
  <div class="forum_post_content">
   <span style="font-size:15px;"><?php echo anchor('profil/'.$query->brugernavn, $query->brugernavn); ?></span>
   <span style="font-size:11px; margin-left:3px; color:#686868;"><i> Siger</i></span><br>
   <?php echo $query->indhold; 
   echo "<br>ID: ".$query->id;
   ?>
  </div><!-- forum_post_content -->


  <?php
  } else {
   echo "Der blev ikke fundet nogen post";
  }
  ?>

</div><!-- forum -->

我的模特

    function posts($id)
 {
  $this->db->select('*');
  $this->db->from('forum_traad');
  $this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad', 'forum_traad.id');
  $this->db->where('forum_traad.id', $id);

  $query = $this->db->get();

  if($query->num_rows > 0)
  {
   return $query->row();
  } else {
   return false;
  }

 }

1 个答案:

答案 0 :(得分:1)

您可以给他们一个不同的名字:

$this->db->select('forum_traad.indhold as traad_indhold,
                   forum_kommentare.indhold as kommentare_indhold');

如果您需要*的功能,您还可以选择:

$this->db->select('forum_traad.indhold as traad_indhold,
                   forum_kommentare.indhold as kommentare_indhold,
                   forum_traad.*,
                   forum_kommentare.*');