【发布时间】:2015-12-16 14:39:59
【问题描述】:
我有如下三个 MySQL 表,(主键突出显示,外键用斜体显示)
项目表,
预计 | 项目名称 |项目描述
项目结构表,
项目结构id | 项目编号 |结构名称
项目任务表,
Projecttasksid | projectstructureid |任务名 |任务开始日期 |任务结束日期 |预计小时数
我有三个数组对象正在传递给页面,
$all_projects - from Projects table,
$all_structures - from Project structure table,
$all_tasks - Project tasks table,
HTML页面如下,
<table id="datatable" class="table table-bordered table-striped table-striped">
<thead>
<tr>
<th>Project</th>
<th>Structure</th>
<th>Task</th>
<th>07</th>
<th>08</th>
<th>09</th>
<th>10</th>
<th>11</th>
</tr>
</thead>
<tbody>
<?php
foreach( $all_projects as $project )
{
foreach( $all_structures as $structure )
{
foreach( $all_tasks as $task )
{
echo '<tr>
<td>'. $project->projectname .'</td>';
if( $structure->projectid == $project->projectid )
{
echo '<td>'. $structure->structurename .'</td>';
}
else
{
echo '<td> </td>';
}
if( $task->projectstructureid == $structure->projectstructureid )
{
echo '<td>'. $task->taskname .'</td>';
}
else
{
echo '<td> </td>';
}
echo '<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>';
}
}
}
?>
</tbody>
</table>
我的问题是,我得到了很多重复的行,如下所示:
我的问题是:如何在一行中显示显示项目名称、结构和任务并避免重复?
【问题讨论】:
-
你不能在一个查询中加入表吗?
-
您所写的查询是重复值出现的唯一原因。使用加入
-
感谢这将尝试加入
标签: php mysql arrays codeigniter