php - 使用 codeigniter 从 2 个表中获取数据

标签 php mysql codeigniter fetch

我得到了这个表:“Study”,“Users”,“Subjects”。

"Study" includes:(id, user_id[is the foreign key to the column "id" of the table "Users"], subject_id[is the foreign key to the column "id" of the table "Subjects"], grade, date)

"Users" includes:(id,username,name,lastname,password,type,status,date)

"Subjects" includes:(id, career_id, name, description, hours)

我想在最后得到这样的东西:

enter image description here

不知道如何在 crudmodel/controller 文件中获取用户名、主题名称:S

这是我的 codeigniter 代码(我的 View 文件):

                    <thead>
                    <th>id</th>
                    <th>User</th>
                    <th>Subject</th>
                    <th>Grade</th>
                    <th>Date</th>
                    <th>Action</th>
                </thead>

<tbody>
    <?php

    if (count($records) > 0 && $records != false) {
        foreach($records as $record) {

            echo "<tr>
                      <td>".$record->id."</td>
                      <td>".$record->user."</td>
                      <td>".$record->subject."</td>
                      <td>".$record->grade."</td>
                      <td>".$record->date."</td>
                      <td align='center'>
                        <a href='".site_url('Home/edit')."/$record->id'> 
                         <button type='button' class='btn btn-primary'>EDIT</button></a> |
                        <a href='".site_url('Home/delete')."/$record->id'> 
                         <button type='button' class='btn btn-danger'>DELETE</button></a>

                  </tr>";
        }

       }
    ?>

</tbody>

这里是我的 Controller 文件:

  class Home extends CI_Controller{

     public function __construct(){
         parent::__construct();

         $this->load->model("Crudmodel");

    }
    public function index(){

        $data['records'] = $this->Crudmodel->getRecords();

        $this->load->view('home', $data);

    }

我的原始模型:

class Crudmodel extends CI_Model{

    public function __construct(){
     parent::__construct();

     $this->load->database();

    }

    public function getRecords(){

        $this->db->select()//DONT KNOW WHAT TO DO
                 ->from()
                 ->join();

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

        if($q -> num_rows() > 0){

            return $q->result();
        }

        return false;

    }

希望你能帮助我

最佳答案

试试这个

在 Controller 中

public function index(){

    # get all data in Study table
    $selectStudys = $this->Crudmodel->selectStudys();

    foreach ($selectStudys as $key => $study) 
    {
        # get UserNames
        $user = $this->Crudmodel->getName($study['user_id']);

        #get Subject Names
        $subject = $this->Crudmodel->getSubName($study['subject_id']);


        #append both NEW VALUES to same array


        $data[$key]['user_id'] = $user[0]['username'];
        $data[$key]['subject_id'] = $subject[0]['name'];

    }

    # Early it was id, Now it has name
    /*
        Example
        ---------
         - Early in user_id 2
         - Now in user_id Mathamatics

    */

    // print_r($data); # check the output

    $data['records'] = $selectStudys;
    $this->load->view('home', $data);

}

在模型中

function selectStudys()
{
    $query= $this->db->query("SELECT * FROM study");
    $result = $query->result_array();
    return $result;
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM Users WHERE id = $name ");
    $result = $query->result_array();
    return $result;
}

function getSubName($subject)
{
    $query= $this->db->query("SELECT name FROM Subjects WHERE id = $subject ");
    $result = $query->result_array();
    return $result;
}

关于php - 使用 codeigniter 从 2 个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43213550/

相关文章:

php - Amazon S3 客户使用 PHP SDK 提供加密

php - 将动态创建的复选框值存储在 Laravel 的另一个表中

CodeIgniter ActiveRecord 乘法

php - 检查是否使用 Laravel 数据透视表设置了某些内容

php - 合并和区分数组数组以找到共同的值

php - Stream_socket_client 无法连接到 Apple APNS(权限被拒绝)

php - 根据 BETWEEN 函数值将 INSERT 上的 DATETIME 值分配给 DB

mysql - Spring boot Multi-Tenancy ,一个租户连接到多个模式

php - 如何从mysql数据库在JPGraph中添加标题

php - Codeigniter PHP 从 mysql 数据库表中提取记录以显示在页面上