[英]select username from different table where id matches in codeigniter
我需要從Login
表中獲取用戶名,注釋來自Report_Comments
表,該表具有FK UserID
,即Login
表中的LoginID
的FK。
因此,如果UserID
為1,則表示LoginID
1。
通過此設置,我知道希望從Login
表中獲取Username
並將其顯示在我的視圖中(在該處顯示“此處為用戶名”)。
我想不出正確的方法,我想到了一個查詢,從Login中選擇Username,其中UserID是LoginID
查詢思路
SELECT `Username`
FROM `Login`
JOIN `Report_Comments`
WHERE `LoginID` = `UserID`
找到正確的查詢后,如何在下面的代碼中進行設置?
模型
function get_comment()
{
$query = $this->db->get('Report_Comments');
return $query->result();
}
視圖
<h1>comments</h1>
<table style="width:100%">
<tr>
<th><h3>Comment</h3></th>
<th><h3>Date</h3></th>
<th><h3>User Name</h3></th>
</tr>
<?php if (isset($reports)) :
foreach ($reports as $row) : ?>
<tr>
<td><?php echo $row->Comments; ?></td>
</tr>
<tr>
<td><?php echo $row->Comment_Date; ?></td>
</tr>
<tr>
<td>username here</td>
</tr>
</table>
<hr>
<?php endforeach; ?>
<?php else : ?>
<p>No Comments</p>
<?php endif; ?>
控制者
function comments()
{
$data = array();
$this->db->where('ReportID', $this->uri->segment(3));
if ($query = $this->report_model->get_comment()) {
$data['reports'] = $query;
}
$this->template['middle'] = $this->load->view($this->middle = 'comments/comment_view', $data, true);
}
將其放在您的模型中:
$this->db->select('Report_Comments.Comment, Report_Comments.Date, Login.Username')
->from('Report_Comments')
->join('Login', 'Report_Comments.UserID = Login.LoginID');
$result = $this->db->get();
然后result
將具有注釋,日期和用戶名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.