gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:01:27 24 4
gpt4 key购买 nike

我得到了这个表:“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com