簡體   English   中英

從不同的表中選擇用戶名,其中id在codeigniter中匹配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM