【发布时间】:2017-04-04 17:09:39
【问题描述】:
我得到了以下表格:“Study”、“Users”、“Subjects”。
“学习”包括:(id,user_id[是表“用户”的“id”列的外键],subject_id[是表“id”列的外键”科目”]、年级、日期)
“用户”包括:(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;
}
希望你能帮助我
【问题讨论】:
-
伙计,我要把所有的“用户名”放在用户栏里,把所有主题的名字放在主题栏里,我该怎么办?
-
对于任何像这样的复杂查询,您应该首先编写一个返回所需结果的 SQL 查询。一旦它起作用了,你就可以相当容易地编写函数了。
-
@Programming_guy 检查以下答案
标签: php mysql codeigniter fetch