这是我的联接表:
模型
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_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; ?>
最佳答案
请尝试以下方法:
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; ?>