简体   繁体   English

如何基于CodeIgniter中另一个表的ID从数据库表中获取结果

[英]How do i get results from a database table based on the id of another table in CodeIgniter

I am have a bit of an issue that I need some pointers on if someone can help me please. 我有一个问题,如果有人可以帮助我,我需要一些指导。

I am using CodeIgniter and need to display the client name, job name and task for each row on the timesheet list daily times are editable in the list but the 3 fields above are static. 我正在使用CodeIgniter,并且需要显示时间表列表中每一行的客户端名称,工作名称和任务,每天在列表中都是可编辑的,但是上面的3个字段是静态的。 Currently i display the id of the client, job and task that are stored in the timesheetEntries table but i need to display the correct names from the seperate tables in each row on the timesheet Entries. 当前,我显示存储在timesheetEntries表中的客户端,作业和任务的ID,但我需要在时间表条目的每一行中显示来自单独表的正确名称。

In its current state it returns corretc results and works well apart from the linking to the other tables. 在其当前状态下,它返回corretc结果,并且除链接到其他表之外还可以很好地工作。

I am going out of my mind on this one as i can't find the solution and am now suffering from code blindness. 我无法解决这一问题,因为我找不到解决方案,现在正遭受代码盲目性的困扰。

Many Thanks 非常感谢

Model 模型

function getAllEntriesByTimesheet($id){
    $data = array();
    $options = array('timesheet_id' => $id);
    $Q = $this->db->get_where('timesheetEntries', $options);
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
    }
    $Q->free_result();    
    return $data; 
}  

Controller 控制者

function id($id){
    $data['title'] = 'Timesheets';
    $data['entry'] = $this->timesheet_model->getAllEntriesByTimesheet($id);
    $data['sheet'] = $this->uri->segment(3);
    $data['header'] = 'default/header';
    $data['main'] = 'timesheet_view';
    $data['footer'] = 'default/footer';
    $data['timesheettab'] = 'current';
    $data['expensestab'] = '';
    $data['holidaytab'] = '';
    $data['admintab'] = '';
    $this->load->vars($data);
    $this->load->view('default/default_view');
}

View 视图

$datestring = '%Y-%m-%d';
if (count($entry)){
    foreach ($entry as $key => $list){
        $dateNow = mdate($datestring);
        $entryId = $list['entry_id'];
        $sheet = $list['timesheet_id'];
        $clientId = $list['client_id'];
        $jobId = $list['job_id'];
        $taskId = $list['task_id'];
        $fridayHrs = $list['friday_hrs'];
        $saturdayHrs = $list['saturday_hrs'];
        $sundayHrs = $list['sunday_hrs'];
        $mondayHrs = $list['monday_hrs'];
        $tuesdayHrs = $list['tuesday_hrs'];
        $wednesdayHrs = $list['wednesday_hrs'];
        $thursdayHrs = $list['thursday_hrs'];
        echo '<tr>'
            .form_open('/timesheet/update_entry')
            .form_hidden('entry_id', $entryId)
            .form_hidden('timesheet_id', $sheet)
            .form_hidden('date_modified', $dateNow).
            '<td>'.$clientId.'</td>
            <td>'.$jobId.'</td>
            <td>'.$taskId.'</td>
            <td class="studio">'.form_input('monday_hrs', $mondayHrs).'</td>
            <td class="studio">'.form_input('tuesday_hrs', $tuesdayHrs).'</td>
            <td class="studio">'.form_input('wednesday_hrs', $wednesdayHrs).'</td>
            <td class="studio">'.form_input('thursday_hrs', $thursdayHrs).'</td>
            <td class="studio">'.form_input('friday_hrs', $fridayHrs).'</td>
            <td class="studio">'.form_input('saturday_hrs', $saturdayHrs).'</td>
            <td class="studio">'.form_input('sunday_hrs', $sundayHrs).'</td>
            <td class="icon">'.form_submit('', 'Update','class="update"').'</td>
            <td class="icon"><a href="/timesheet/edit_entry/'.$sheet.'/'.$entryId.'"><img src="/images/sitewide/edit.png" alt="Edit Entry" width="24" height="24" /></a></td>
            <td class="icon"><a href="/timesheet/delete_entry/'.$sheet.'/'.$entryId.'"><img src="/images/sitewide/delete.png" alt="Delete Entry" width="24" height="24" /></a></td>'
            .form_close()
            .'</form></tr>';    
    }
}

These are the database table schema for related tables 这些是相关表的数据库表架构

tasks 任务

+------------------+-------------------+--------+------+----------+--------+
| Field            | Type              | Null   | Key  | Default  | Extra  |
+------------------+-------------------+--------+------+----------+--------+
| task_id          | int(4) unsigned   | NO     | PRI  | NULL     | AI     |
| task_name        | varchar(100)      | NO     |      | NULL     |        |
| task_type        | int(2)            | NO     |      | 2        |        |
| task_code        | varchar(10)       | NO     |      | NULL     |        |
+------------------+-------------------+--------+------+----------+--------+

clients 客户

+------------------+-------------------+--------+------+----------+--------+
| Field            | Type              | Null   | Key  | Default  | Extra  |
+------------------+-------------------+--------+------+----------+--------+
| client_id        | int(4) unsigned   | NO     | PRI  | NULL     | AI     |
| client_name      | varchar(100)      | NO     |      | NULL     |        |
+------------------+-------------------+--------+------+----------+--------+

jobs 工作

+------------------+-------------------+--------+------+----------+--------+
| Field            | Type              | Null   | Key  | Default  | Extra  |
+------------------+-------------------+--------+------+----------+--------+
| job_id           | int(6) unsigned   | NO     | PRI  | NULL     | AI     |
| job_number       | varchar(6)        | NO     |      | NULL     |        |
| client_id        | int(4)            | NO     |      | NULL     |        |
| job_name         | varchar(100)      | NO     |      | NULL     |        |
| create_date      | date              | NO     |      | NULL     |        |
| start_date       | date              | NO     |      | NULL     |        |
| account_handler  | varchar(4)        | NO     |      | NULL     |        |
| studio_resource  | varchar(4)        | NO     |      | NULL     |        |
| action           | varchar(50)       | NO     |      | NULL     |        |
| by_whom          | varchar(10)       | NO     |      | NULL     |        |
| deadline         | date              | NO     |      | NULL     |        |
| job_status       | varchar(50)       | NO     |      | NULL     |        |
+------------------+-------------------+--------+------+----------+--------+

timesheetEntries 时间表条目

+------------------+-------------------+--------+------+----------+--------+
| Field            | Type              | Null   | Key  | Default  | Extra  |
+------------------+-------------------+--------+------+----------+--------+
| entry_id         | int(10) unsigned  | NO     | PRI  | NULL     | AI     |
| user_id          | int(10)           | NO     |      | NULL     |        |
| timesheet_id     | int(5)            | NO     |      | NULL     |        |
| client_id        | int(4)            | NO     |      | NULL     |        |
| job_id           | int(4)            | NO     |      | NULL     |        |
| task_id          | int(2)            | NO     |      | NULL     |        |
| monday_hrs       | decimal(4,2)      | NO     |      | 0.00     |        |
| tuesday_hrs      | decimal(4,2)      | NO     |      | 0.00     |        |
| wednesday_hrs    | decimal(4,2)      | NO     |      | 0.00     |        |
| thursday_hrs     | decimal(4,2)      | NO     |      | 0.00     |        |
| friday_hrs       | decimal(4,2)      | NO     |      | 0.00     |        |
| saturday_hrs     | decimal(4,2)      | NO     |      | 0.00     |        |
| sunday_hrs       | decimal(4,2)      | NO     |      | 0.00     |        |
| total_hrs        | decimal(4,2)      | NO     |      | 0.00     |        |
| date_entered     | date              | NO     |      | 0.00     |        |
| date_modified    | date              | NO     |      | 0.00     |        |
+------------------+-------------------+--------+------+----------+--------+

Solution

Provided by András Rátz 由AndrásRátz提供

Model 模型

function getAllEntriesByTimesheet($id){
    $data = array();
    $this->db->select('*');
    $this->db->join('tasks', 'tasks.task_id = timesheetEntries.task_id', 'left');
    $this->db->where('timesheetEntries.timesheet_id',$id);
    $Q = $this->db->get('timesheetEntries');
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
    }
    $Q->free_result();    
    return $data; 
} 

the View need to have the variable 视图需要有变量

$taskId = $list['task_id']; 

changed to 变成

$taskId = $list['task_name'];

sorted thanks :) 排序谢谢:)

Maybe it's just me, but this seems to be more geared toward the database side of things. 也许只有我一个人,但这似乎更适合于数据库方面。 Without knowing the database structure, it'd be hard to give much more than to suggest maybe creating a view within the database that gives you what you want and then query and return data from this view. 在不了解数据库结构的情况下,很难提供太多建议,而不是建议在数据库中创建一个视图,该视图为您提供所需的内容,然后从该视图中查询并返回数据。

Because you didn't wrote the table names, here is an example. 因为您没有写表名,所以这里有个例子。 You need to change your model. 您需要更改模型。

$this->db->select('*');
$this->db->from('timesheetEntries');
$this->db->join('otherTable', 'otherTable.id = timesheetEntries.taskId', 'left');
$this->db->where('timesheetEntries.timesheet_id',$id);
$Q = $this->db->get();

In $this->db->get(), leave blank the get part, because with from, you already selected the table. 在$ this-> db-> get()中,将get部分保留为空白,因为使用from时,您已经选择了表。 Without the exact table structure, it's hard to write the correct syntax. 没有确切的表结构,很难编写正确的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据cakephp中其他表的结果从数据库表中获取结果? - How do I get results from a database table based on results from a different table in cakephp? 如何使用MySQL Left Join根据另一个表行的id获取结果? - How do I use MySQL Left Join to get results based on another table row's id? 如何在Codeigniter中基于手机号码从两个表中获取结果 - How to get the results from two table based on mobile number in codeigniter 如何基于另一个表ID Codeigniter更新一个表 - how to update one table based on another table ID codeigniter 如何从Codeigniter中的数据库表中获取数据? - How to get data from database table in codeigniter? 如何使用Codeigniter从表中获取最后3行? - how do i get the 3 last rows from a table using codeigniter? 从一个表中获取具有特定ID的列的总和,并在Codeigniter中从具有该ID的另一张表中获取详细信息 - Get the sum of the colunm with specific id from one table and get the details from another table with that id in codeigniter 如何使用Eloquent基于另一个表获取行? - How do I get rows based on another table with Eloquent? 如何根据另一个字符串的值获取表值? - How do I get a table value based on the value of another string? 如何从同一表获取基于parent_id的行以与其他行相对应 - How do I get rows to correspond to other rows based on a parent_id , all from the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM