删除foreach中的重复值

时间:2014-09-10 05:05:12

标签: php mysql codeigniter foreach

Hello Developers我必须在我的应用程序中打印报告。我正在使用codeigniter。我的数据库中有这样的表格。

Report Detail
---------------------------------------------------------------------------
ID   Test_ID      Description    Description_Group       Test_Name
---------------------------------------------------------------------------
1    117          value 1         group 1                Test 1
2    117          value 2         group 1                Test 1
3     4           another 1       group 2                Test 2
4     4           another 2       group 2                Test 2
5     4           another 3       group 2                Test 2

我想像这样打印

 desired print format
    -----------------------------------------------
    Description
    -----------------------------------------------
    **Test 1**
    group 1
      value 1
      value 2

   **Test 2**
   group 2
     another 1
     another 2
     another 3

这是我的Model Codel

public function print_report($Patient_ID, $ID)
{

    $query = $this->db->query('select a.ID, a.Description, a.Description_Group, a.Normal_Range, a.Measure_Unit,
                               b.Result_Value
                               from tbltestdefault a
                               inner join tblreportdetail b on a.ID = b.Test_Default_ID
                               where b.Patient_ID = '.$Patient_ID.' and b.Patient_Test_ID  = '.$ID.'');
    return $query->result_array();
}

控制器

public function print_report($ID, $Patient_ID, $Test_ID)
{
    $data['patient'] = $this->Report_Model->getPatientinfo($Patient_ID);
    $data['testname'] = $this->Report_Model->gettestnameonly($Test_ID);
    $data['tests'] = $this->Report_Model->print_report($Patient_ID, $ID);
    $this->load->view('report_view', $data);
}

和显示数据的视图

<table class="table table-bordered">
        <thead>
            <tr>
                <th> <h4>Description</h4>
                </th>
                <th> <h4>Result</h4>
                </th>
                <th> <h4>Units</h4>
                </th>
                <th> <h4>Normal Value</h4>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($tests as $t): ?>

            <tr>
                <td>
                    <?php echo $t['Description_Group']; ?>
                    <?php echo $t['Description']; ?>
                </td>
                <td><?php echo $t['Result_Value']; ?></td>
                <td><?php echo $t['Measure_Unit']; ?></td>
                <td><?php echo $t['Normal_Range']; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

1 个答案:

答案 0 :(得分:1)

男人,我认为你认为这个网站是理所当然的..我已经为你提供了主要的逻辑。你只需要根据你的额外需求扩展它。顺便说一句,这是扩展的逻辑

<?php
$get_title = '';
$get_subtitle = '';
foreach($tests as $t)
{
    //if start of new test name
    if($get_title != $t['Test_Name'])
    {
        echo $t['Test_Name'];

        //if start of new desc group
        if($get_subtitle != $t['Description_Group'])
        {
            echo $t['Description_Group'];
            echo $t['description'];
            $get_subtitle = $t['Description_Group'];
        }
        //if same desc with previous
        elseif($get_subtitle == $t['Description_Group'])
        {
            echo $t['description'];
        }
        $get_title = $t['Test_Name'];
    }
    //if same title with previous
    elseif($get_title == $t['Test_Name'])
    {
        //if start of new desc group
        if($get_subtitle != $t['Description_Group'])
        {
            echo $t['Description_Group'];
            echo $t['description'];
            $get_subtitle = $t['Description_Group'];
        }
        //if same desc with previous
        elseif($get_subtitle == $t['Description_Group'])
        {
            echo $t['description'];
        }
    }
}

在sql查询的最后部分添加它    ORDER BY Test_Name ASC,Description_Group ASC,描述ASC 不确定这是否有效,因为我没有尝试过......如果出现问题,可以给我一些评论......工作人员