我得到了这些表:"研究","用户","主题"。
"研究"包括:(id,user_id [是列的外键" id"表"用户"],subject_id [是列的外键&#34 ;表"表#34;主题"],成绩,日期)
"用户"包括:的(ID,用户名,名,姓,密码,类型,状态,日期)
"主体"包括:(id,career_id,名称,描述,小时)
我想在最后得到这样的东西:
不知道在crudmodel / controller文件中做什么来获取用户名,主题名称:S
这是我的codeigniter代码(我的视图文件):
<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>
这是我的控制器文件:
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);
}
我的crudmodel:
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;
}
希望你能帮助我
答案 0 :(得分:0)
试试这个
在控制器中
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;
}