来自连接表的错误回声数据

时间:2014-07-18 11:58:41

标签: php mysql join

这是我的联接表:

模型

public function blog_list()
{
    $this->db->select('b.id, bl.id_p, u.name, bl.cat2, b.vote');
    $this->db->join('blog b', 'bl.id_p = b.id_p');
    $this->db->join('users u', 'u.id = bl.id_p');
    $this->db->from('blog_list bl');

$this->db->get();
}

我的问题是,当我尝试回显来自列的所有记录时,例如id_p,我从blog表而不是blog_list获取此记录。我尝试使用blog_lists反向博客并尝试不同类型的连接表,而不期待结果。

控制器:

public function index()
{
    $data['blog_list'] = $this->users_m->blog_list();
    $where = "id_p = " . $this->session->userdata('id');
    $data['user'] = $this->users_m->get('blog_list',$where,TRUE);
    $this->load->view('blog/blog_list', $data);
}

视图:

    <?php foreach($blog_list as $row): ?>
          <?php echo ($row->name); ?> 
          <?php echo ($row->id); ?> 
          <?php echo ($row->id_p); ?> 
          <?php echo ($row->vote); ?> 
          <?php echo ($row->cat2); ?> 
          <?php echo anchor('blogs/blog/' . $row->id_p , 'Odwiedź >>'); ?> 
          <br><br>
    <?php endforeach; ?>

1 个答案:

答案 0 :(得分:1)

请试试这个:

public function blog_list()
{
    $this->db->select('b.id_p, bl.id_p, u.name, bl.cat2, b.vote');
    $this->db->from('blog_list bl');
    $this->db->join('blog b', 'b.id_p = bl.id_p');
    $this->db->join('users u', 'u.id = bl.id_p');
    $query = $this->db->get();
}

更新

public function blog_list()
{
    $this->db->select('b.id_p AS blog_id, 
                       bl.id_p AS blog_list_id, 
                       u.name AS user_name, 
                       bl.cat2 AS blog_list_cat, 
                       b.vote AS blog_vote');
    $this->db->from('blog_list bl');
    $this->db->join('blog b', 'b.id_p = bl.id_p');
    $this->db->join('users u', 'u.id = bl.id_p');
    $query = $this->db->get();
}

在代码中:

<?php foreach($blog_list as $row): ?>
      <?php echo ($row->user_name); ?> 
      <?php echo ($row->blog_id); ?> 
      <?php echo ($row->blog_list_id); ?> 
      <?php echo ($row->blog_vote); ?> 
      <?php echo ($row->blog_list_cat); ?> 
      <?php echo anchor('blogs/blog/' . $row->blog_list_id , 'Odwiedź >>'); ?> 
      <br><br>
<?php endforeach; ?>