如何根据CodeIgniter中另一个表的id从数据库表中获取结果

时间:2012-08-13 16:38:04

标签: php codeigniter

我有一个问题,如果有人可以帮助我,我需要一些指示。

我正在使用CodeIgniter并且需要在时间表列表中显示每行的客户端名称,作业名称和任务,每天都可以在列表中编辑,但上面的3个字段是静态的。目前我显示存储在timesheetEntries表中的客户端,作业和任务的id,但我需要在时间表条目的每一行的单独表格中显示正确的名称。

在当前状态下,它会返回corretc结果,并且与链接到其他表格之间的效果很好。

由于我无法找到解决方案,我现在已经忘记了这个问题,现在我正在遭遇代码盲。

非常感谢

模型

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; 
}  

控制器

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');
}

查看

$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>';    
    }
}

这些是相关表的数据库表模式

任务

+------------------+-------------------+--------+------+----------+--------+
| 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     |        |
+------------------+-------------------+--------+------+----------+--------+

客户端

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

作业

+------------------+-------------------+--------+------+----------+--------+
| 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     |        |
+------------------+-------------------+--------+------+----------+--------+

解决方案

由AndrásRátz提供

模型

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; 
} 

View需要变量

$taskId = $list['task_id']; 

更改为

$taskId = $list['task_name'];

排序谢谢:)

2 个答案:

答案 0 :(得分:0)

也许只是我,但这似乎更倾向于数据库方面。在不知道数据库结构的情况下,除了建议在数据库中创建一个能够提供所需内容然后从该视图中查询和返回数据的视图之外,很难给出更多信息。

答案 1 :(得分:0)

因为您没有写表名,所以这是一个例子。你需要改变模型。

$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();

在$ this-&gt; db-&gt; get()中,将get部分留空,因为使用from,您已经选择了表格。 如果没有确切的表结构,则很难编写正确的语法。