大家好,我正在使用codeigniter構建項目管理系統。數據庫多表查詢和顯示結果
我在我的模型中有一個函數來顯示一個項目和與之相關的任務。 $ projectId = 1僅用於測試目的。
function getAllProjects($projectId = 1)
{
$this->db->select('*');
$this->db->from('projects');
$this->db->where('projects.projectId', $projectId);
$this->db->join('projectTasks', 'projects.projectId = projectTasks.projectId');
$this->db->join('tasks', 'projectTasks.taskId = tasks.taskId');
$projects = $this->db->get();
return $projects;
}
然後在我看來,我把它顯示結果
<?php if($projects->num_rows() > 0): ?>
<table width="100%">
<?php foreach($projects->result() as $p): ?>
<tr>
<td><?php echo $p->projectId; ?></td>
<td><?php echo $p->projectName; ?></td>
<?php foreach($projects->result() as $g): ?>
<td><?php echo $g->taskName; ?></td>
<td><?php echo $g->taskHours; ?></td>
<td><?php echo $g->taskCost; ?></td>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>No projects at this time.</p>
<?php endif; ?>
它返回這個
ID - Project Name - Task Name - Task Hours - Task Cost (**to show you what field is what**)
1 The Range 702 Contact Form 10 100 Custom Logo Desgin 10 100 Custom Login From 5 75
1 The Range 702 Contact Form 10 100 Custom Logo Desgin 10 100 Custom Login From 5 75
1 The Range 702 Contact Form 10 100 Custom Logo Desgin 10 100 Custom Login From 5 75
可有人請告訴我如何使它所以它只顯示項目信息一次,然後是與項目關聯的每個任務。現在,對於與項目相關的每項任務,都會列出項目及其所有任務。
表
-----------------------
| projects |
-----------------------
| projectId | (Primary)
| projectName |
| projectHours |
| projectDeadline |
| projectStartDate |
| projectTasks |
| projectUsers |
| projectNotes |
| |
-----------------------
-----------------------
| projectTasks |
-----------------------
| projectTasksId | (Primary)
| projectId | (FK project->projectId)
| taskId | (FK tasks->taskId)
| |
-----------------------
--------------------
| tasks |
--------------------
| taskId | (Primary)
| taskName |
| taskHours |
| taskCost |
| |
--------------------
我把這段代碼放到我的視圖中,它完美地工作。非常感謝你幫助我。 – Hayden
不客氣,很高興我可以幫助。 –