大家好我正在使用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 :(得分: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; ?>
答案 1 :(得分: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显示一次。