使用 codeigniter 替换外键的 "values"

Replace the "values" of the foreign key using codeigniter

我制作了一个获取 2 tables 值的程序。我的意思是我的程序 只获取 tables 的 "id" (外键)但它不显示与该 ID 对应的值。

我的tables:

"cursadas" includes:(id, user_id[is the foreign key to the column "id" of the table "usuarios"], subject_id[is the foreign key to the column "id" of the table "materias"], grade, date)

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

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

看看那个,我的程序显示这个 table:

我的目标是展示这个 table(绿色的):

不知道该怎么做,这是我的代码:

我的查看文件("home"):

    <html>

    <head>



    </head>

<body>

    <div class="container"> 
    <div class="row">
    <div class="col-md-12">

        <h2 align="center">TABLE:Study</h2>

        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <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_id']."</td>
                      <td>".$record['subject_id']."</td>
                      <td>".$record['grade']."</td>
                      <td>".$record['date']."</td>
                      <td align='center'>

                         <button type='button' class='btn btn-primary'>EDIT</button></a> |

                         <button type='button' class='btn btn-danger'>DELETE</button></a>

                  </tr>";
        }

       }
    ?>

</tbody>

    </table>

        </div>
        </div>
        </div>

</body>
</html>

我的控制器文件 ("Home")

    <?php

    class Home extends CI_Controller{

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

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

        }


 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

        if(!empty($user[0]['username'])){
        $data[$key]['user_id'] = $user[0]['username'];

        }else{
         $data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler 
        }
        if(!empty($subject[0]['name'])){
        $data[$key]['subject_id'] = $subject[0]['name'];

        }else{
           $data[$key]["subject_id"] = "";
          // or anything you can use as error handler
        }

    }


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

}

}
?>

我的模型文件:

       <?php

    class Crudmodel extends CI_Model{

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

         $this->load->database();

        }


function selectStudys()
{
    $query= $this->db->query("SELECT * FROM cursadas");
    if($query->num_rows()>0){
       $result = $query->result_array();
     }else{
      $result = array();
    }
    return $result;
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
    if($query->num_rows()>0){
    $result = $query->result_array();
    }else{
    $result = "";
    }
          // or anything you can use as error handler
    return $result;
  }


function getSubName($subject)
{
    $query= $this->db->query("SELECT name FROM materias WHERE id = $subject ");
  if($query->num_rows()>0){
     $result = $query->result_array();
  }else{
      $result = "";
          // or anything you can use as error handler
  }
      return $result;
}


 function CombineResults($subject, $name){
  // you can also use this
   $query = $this->db->query("SELECT sub.name, user.username FROM materias sub, Users user WHERE sub.id=$subject AND user.id = $name");
   if($query->num_rows()>0){
        return $query->result();
         }else{
           return array();
              // or anything you can use as error handler
        }
    }       
  }
    ?>

我很奇怪你为什么要使用这么长的脚本,你可以简单地使用 MySQL 加入。

这是查询

SELECT a.id ID,b.username User, c.name, a.grade, a.date
FROM cursadas a 
JOIN usuarios b ON a.user_id=b.id 
JOIN materias c ON c.id=a.subject_id

这是 SQLFIDDLE 的 link。 Click Here

模型代码:

function your_function_name()
{
    $result = NULL;
    $query=$this->db->query('SELECT a.id ID,b.username User, c.name, a.grade, a.date FROM cursadas a JOIN usuarios b ON a.user_id=b.id JOIN materias c ON c.id=a.subject_id');
     if($query->num_rows()>0){
        $result = $query->result_array();
      }
    return $result;
}

控制器代码:

function your_controller_function()
{
  $this->load->model('your_model');
  $data['records'] = $this->your_model->your_function_name();
  $this->load->view('home', $data);
}

查看代码:

<table>
<tr><th>ID</th><th>User</th><th>Subject</th><th>Grade</th><th>Date</th><th>Action</th></tr>
<?php
if($records)
{
  foreach($records as $rc)
  {
   <tr><td><?php echo $c->id;?></td>
   <td><?php echo $c->username;?></td>
   <td><?php echo $c->name;?></td>
   <td><?php echo $c->grade;?></td>
   <td><?php echo $c->date;?></td>
   <td><a href="#">Edit</a><a href="#">Delete</a></td></tr>
   }
}
?>
</table>