gpt4 book ai didi

php - Codeigniter - 在 HTML 表中显示来自三个表的数据作为数组对象

转载 作者:行者123 更新时间:2023-11-29 21:43:16 26 4
gpt4 key购买 nike

我有如下三个MySQL表,(主键突出显示,外键斜体)

项目表,

预计 | 项目名称 |项目描述

项目结构表,

项目结构ID | 项目ID |结构名称

项目任务表,

Projecttasksid | 项目结构ID |任务名称 |任务开始日期 |任务结束日期 |预计时间

我有三个数组对象传递到页面,

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

我的问题是,我收到了很多重复行,如下所示:

image

我的问题是:如何在一行中显示项目名称、结构和任务并避免重复?

最佳答案

首先这是codeigniter,我可以使用模型中的联接来解决此问题,而不是单独传递

$all_projects - from Projects table,
$all_structures - from Project structure table,
$all_tasks - Project tasks table,

我创建了一个这样的模型,

function project_structure_task()
{

$this->db->select('projects.projectid, projects.projectname,
projectstructure.projectstructureid,
projectstructure.structurename,
projecttasks.* ')->from('projectstructure')
->join('projecttasks', 'projecttasks.projectstructureid = projectstructure.projectstructureid')
->join('projects', 'projects.projectid = projectstructure.projectid');

$sql_stmt = $this->db->get();
return $sql_stmt->result();
}

并在 Controller 上调用它并像数组一样传递,

$data = array(  'title'         => 'My title',
'project_tasks' => $this->timesheet_model->project_structure_task() );

$this->load->view('template', $data);

并在 View 中使用 foreach 来使用它

foreach( $project_tasks as $task )
{ }

关于php - Codeigniter - 在 HTML 表中显示来自三个表的数据作为数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com