2013-10-24 82 views
1

大家好,我正在使用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  | 
|     | 
-------------------- 

回答

0

雖然我認爲Jorge Campos解決方案很近,但它不會阻止任務被多次列出。

<?php $ProjectId = ''; ?> 
     <?php if($projects->num_rows() > 0): ?> 
     <table width="100%"> 
      <?php foreach($projects->result() as $p): ?> 
      <tr> 
       <?php if ($ProjectId != $p->projectId) { ?> 
       <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; ?> 

      <?php } ?> 
     <?php $ProjectId = $p->projectId; ?> 

      </tr> 
      <?php endforeach; ?> 
     </table> 
     <?php else: ?> 
     <p>No projects at this time.</p> 
     <?php endif; ?> 

該解決方案將使信息僅顯示一次projectId。

+0

我把這段代碼放到我的視圖中,它完美地工作。非常感謝你幫助我。 – Hayden

+0

不客氣,很高興我可以幫助。 –

0

創建一個變量來存儲項目ID爲每一次迭代,那麼你有實際的項目ID進行比較,如果是不同你展示項目信息。像這樣的東西。

<?php 
$beforeProject = ''; 
foreach($projects->result() as $p): 
?> 
    <td><?php if ($beforeProject != $p->projectId) { echo $p->projectId; } ?></td> 

    //rest of code 

    <?php $beforeProject = $p->projectId; ?> 
<?php endforeach; ?>