【问题标题】:Codeigniter - Show data in HTML table that coming from three tables as array objectCodeigniter - 在 HTML 表格中显示来自三个表格的数据作为数组对象
【发布时间】: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


【解决方案1】:

首先这是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();
}

并在控制器上调用它并像数组一样传递,

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

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

并通过 foreach 在视图中使用它

foreach( $project_tasks as $task )
{ }

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2019-12-05
    • 2012-12-20
    • 1970-01-01
    • 2017-12-02
    • 2011-02-15
    相关资源
    最近更新 更多