foreach循环重复相同的值

时间:2015-03-30 10:36:27

标签: codeigniter foreach

我从数据库中的两个表中获取结果。表1具有项目名称,表2具有项目名称下的子项目名称,这两个表由id连接。 现在当我使用foreach通过连接两个表输出结果时,它显示重复显示项目名称,因为在该重复项目下有多个子项目。 我希望项目名称是单一的,子项目结果将在主项目名称下循环。怎么做?

我在使用codeigniter进行开发。

我的观看页面:

<?php foreach ($reportlist as $project_item): { ?>
<tr>
<td>
<form method='post' action='update_project'>
<input type='hidden' value='<?php echo ($project_item['pid']); ?>' name='pid'>
<input type='hidden' value='<?php echo $project_item['pp_id']; ?>' name='pp_id'>
<input type="submit" class="linkButton"  value="<?php echo $project_item['p_name'] ?>">
</form>
</td>

<td>
<form method='post' action='update_subproject'>
<input type='hidden' value='<?php echo $project_item['pid']; ?>' name='pid'>
<input type='hidden' value='<?php echo $project_item['pp_id']; ?>' name='pp_id'>
<input type="submit" class="linkButton"  value="<?php echo $project_item['component_name'] ?>" >
</form>
</td> 


    <td><?php echo $project_item['coordinating_authority'] ?></td> 
    <td><?php echo $project_item['start_month'] ?></td>
    <td><?php echo $project_item['target_month'] ?></td>
    <td><?php echo $project_item['stat_pro'] ?></td>
    <td><?php echo $project_item['stat_pro'] ?></td>
    <td><?php echo $project_item['remark_pro'] ?></td>

</tr>
<?php } endforeach ?>
</tbody>

我的cotroller页面:

  $this->load->model('projsubproj_add_model');
  $data['reportlist'] = $this->projsubproj_add_model->existing_project();
  $this->load->view('PAGE-DESIGN/adminheader');
  $this->load->view('existing_project_view', $data);
  $this->load->view('PAGE-DESIGN/footer');

和我的模特:

public function existing_project()
{
{
$this->db->select('project_tab.p_name,
pp_project_tab.pp_id AS pp_id,
pp_project_tab.pid AS pid,
pp_project_tab.ppname AS component_name, 
pp_project_tab.pp_smonth AS start_month,
pp_project_tab.pp_emonth AS target_month,
pp_project_tab.pp_cordn_reqrd AS coordinating_authority,
pp_project_tab.pps_status AS stat_pro,
pp_project_tab.remarks AS remark_pro');
 $this->db->from('project_tab');
        $this->db->join('pp_project_tab', 'pp_project_tab.pid =project_tab.pid');
        //$this->db->join('pp_status_tab', 'pp_status_tab.pp_id = pp_project_tab.pp_id');
        $this->db->order_by('ppname', 'desc');
        $query = $this->db->get();

        return $query->result_array();
    }

    $query = $this->db->get_where('project_tab');
    return $query->row_array();
}

1 个答案:

答案 0 :(得分:0)

我得到了解决方案:我刚刚通过foreach循环编辑了我的视图文件。我做了两个函数来从两个表中提取结果并加入它们,条件是如果子项目表的pid与subject表的pid匹配,那么echo子项目名称及其组件在Project循环的foreach循环中。

这是我的代码:

$ projects用于从project_tab中提取数据,
$ components用于从pp_project_tab中提取数据

<?php

foreach ($projects as $project) {

echo $project['p_name'];

foreach ($components as $component) {
    if ($project['pid']==$component['pid']) {
        echo  $component['ppname'] ; 

        }


   }



}

&GT;