按类别对数据库中的数据进行分组

时间:2019-04-15 06:14:05

标签: php codeigniter

我在下面的数组中有来自sql查询的记录,我想按数组中的标题元素显示记录。

在下面的数组中,我有两条传真2 title的记录,然后它应该显示fax2的一个表标题并显示与传真2title相关的所有行。

请找到下面的屏幕以显示当前显示结果 enter image description here

Array ( [0] => Array ( [title] => Fax 1 [workflow_id] => 152623 [company_name] => gord's test account inc. [opener] => Adam Guzzo [phone] => 514-572-2326 ) 

[1] => Array ( [title] => Fax 2 [workflow_id] => 152624 [company_name] => gord's test account inc. [opener] => Adam Guzzo [phone] => 514-572-2326 ) 

[2] => Array ( [title] => Fax 2 [workflow_id] => 953707 [company_name] => gord's test account inc. [opener] => Adam Guzzo [phone] => 514-572-2326 ) )
 <?php foreach($previousFaxes as $previousFaxesData) :  ?>

        <table width="100%" border="0" cellspacing="0" cellpadding="3" class="tablesorter">
            <thead>
                <tr>
                    <th align="left" colspan="8"><?php echo $previousFaxesData['title'];?></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td align="left">Workflow</td>
                    <td align="left">Phone</td>
                    <td align="left">Opener</td>
                </tr>  
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>   
<?php  endforeach; ?>

1 个答案:

答案 0 :(得分:0)

按照下面的代码,我试图使它像您想要的那样,它将按您预期的那样在屏幕截图中创建结果,但是要确保数组结果只包含所需的字段,因此必须注意。

它是基于您的数组的动态。

输出类似:https://prnt.sc/nc1rrq

<?php
  $array = Array (
    Array  (
      "title" => "Fax 1,",
      "workflow_id" => "152623,",
      "company_name" => "gord's test account inc.",
      "opener" => "Adam Guzzo",
      "phone" => "514-572-2326",
    ),

    Array  (
      "title" => "Fax 2,",
      "workflow_id" => "152624,",
      "company_name" => "gord's test account inc.",
      "opener" => "Adam Guzzo",
      "phone" => "514-572-2326",
    ),

    Array  (
      "title" => "Fax 2,",
      "workflow_id" => "953707,",
      "company_name" => "gord's test account inc.",
      "opener" => "Adam Guzzo",
      "phone" => "514-572-2326",
    )
  );
?>
<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <table border="1" cellpadding="2">
    <thead>
    </thead>
    <tbody>
      <?php foreach ($array as $key => $value) {?>
      <tr bgcolor="#ccc">
        <td colspan="4"><?php echo $value['title']; ?></td>
      </tr>
      <tr>
        <?php foreach ($value as $k => $val){?>
          <?php if ($k == "title") {continue; } ?>
          <td><input type="text" name="" value="" placeholder=""></td>
        <?php } ?>
      </tr>

      <tr>
        <?php foreach ($value as $k => $val){?>
          <?php if ($k == "title") {continue; } ?>
          <td><?php echo ucfirst(str_replace('_', ' ', $k)); ?></td>
        <?php } ?>
      </tr>
      <tr>
        <td colspan="4"><hr></td>
      </tr>

      <?php }?>
    </tbody>
  </table>
</body>
</html>