【问题标题】:remove duplicate values in foreach删除foreach中的重复值
【发布时间】:2014-11-03 15:27:13
【问题描述】:

您好开发人员我必须在我的应用程序中打印一份报告。我正在使用代码点火器。我的数据库中有一张这样的表。

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

这是我的模型代码

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>

【问题讨论】:

  • 好的,很酷。到目前为止,您为实现这一目标做了什么尝试? (我们不会为您编写代码,我们只是在这里帮助您找出尝试的代码为什么不起作用)
  • 我只是用 foreach 从表中获取所有数据并按原样显示..
  • 不要告诉我这就是你在做什么,向我们展示你使用的代码,产生了什么,以及为什么你觉得这不是你想要的。已编辑到您的帖子中(未作为评论回复)
  • 是的,当然..现在请看一下
  • 这是非常好的附加信息;仍然缺少一点是它现在产生的让你觉得它做错事的东西。它是否显示正确的数据,格式错误?还是它完全显示了错误的数据?

标签: php mysql codeigniter foreach


【解决方案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、Description ASC 不确定这是否可行,因为我还没有尝试过.. 如果出现问题,请用一些 cmets 打我.. 干杯

【讨论】:

  • @ShafatAhmad 如果您满意,您可以接受我的回答。谢谢:D
  • 你好@Kelvin Barsana 你在吗?
猜你喜欢
  • 1970-01-01
  • 2018-07-26
  • 2017-09-12
  • 2014-12-10
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2017-02-23
相关资源
最近更新 更多