为了账号安全,请及时绑定邮箱和手机立即绑定

如何按日期对数据进行分组并使用php在html表中显示

如何按日期对数据进行分组并使用php在html表中显示

PHP
泛舟湖上清波郎朗 2021-04-26 17:18:08
您好,我是编程的新手,我偶然发现了按日期对数据进行分组并将其显示在带有日期分隔符的表格中的情况。这是mysql数据:如何将数据分组为日期,并将其显示在html表中,如下所示。 我知道如何从数据库中获取数据并将它们显示在html表中,但是按日期对数据进行分组对我来说似乎并不容易,我是php编程的初学者。我将不胜感激。
查看完整描述

1 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

显示输出:

//img1.sycdn.imooc.com//609e1d3400010bf512690134.jpg

创建一个提供了数据的数组并遍历该数据。


关键是使用数组的关键值。


主数组应具有date一个键,并将所有员工行附加到该键上。


数组结构应为:


Array

(

    [19/04/19] => Array

        (

            [0] => Array

                (

                    [id] => 1

                    [idno] => 111

                    [date] => 19/04/19

                    [employee] => MAYER CU

                    [timein] => 8:00:00 AM

                    [timeout] => 6:54:00 PM

                    [totalhours] => 10:54

                )


            [1] => Array

                (

                    [id] => 2

                    [idno] => 111

                    [date] => 19/04/19

                    [employee] => MAYER CU

                    [timein] => 10:00:00 AM

                    [timeout] => 6:00:00 PM

                    [totalhours] => 8

                )


        )


    [20/04/19] => Array

        (

            [0] => Array

                (

                    [id] => 3

                    [idno] => 111

                    [date] => 20/04/19

                    [employee] => MIKE SMITH

                    [timein] => 9:01:00 AM

                    [timeout] => 6:54:00 PM

                    [totalhours] => 9:53

                )


            [1] => Array

                (

                    [id] => 4

                    [idno] => 111

                    [date] => 20/04/19

                    [employee] => MIKE SMITH

                    [timein] => 10:00:00 AM

                    [timeout] => 6:55:00 PM

                    [totalhours] => 8:55

                )


        )


)

代码:


<?php 

$arr = [];

$arr[] = ['id' => 1, 'idno' => 111, 'date'=> '19/04/19', 'employee' => 'MAYER CU', 'timein' => '8:00:00 AM', 'timeout' => '6:54:00 PM', 'totalhours' => '10:54'];

$arr[] = ['id' => 2, 'idno' => 111, 'date'=> '19/04/19', 'employee' => 'MAYER CU', 'timein' => '10:00:00 AM', 'timeout' => '6:00:00 PM', 'totalhours' => '8'];

$arr[] = ['id' => 3, 'idno' => 111, 'date'=> '20/04/19', 'employee' => 'MIKE SMITH', 'timein' => '9:01:00 AM', 'timeout' => '6:54:00 PM', 'totalhours' => '9:53'];

$arr[] = ['id' => 4, 'idno' => 111, 'date'=> '20/04/19', 'employee' => 'MIKE SMITH', 'timein' => '10:00:00 AM', 'timeout' => '6:55:00 PM', 'totalhours' => '8:55'];


$arrDisplay = [];

if (! empty($arr)) {

 foreach ($arr as $employee) {

  $arrDisplay[$employee['date']][] = $employee;

 }

}

//echo '<pre>';print_r($arrDisplay);echo '</pre>';

?>

<table border="1" width="100%" cellspacing="0" cellpadding="0">

<?

if (! empty($arrDisplay)) {

 foreach ($arrDisplay as $date => $employees) {

 ?>

 <tr>

<td colspan="7" align="center"><?php echo $date;?></td>

</tr>

<?

if (! empty($employees)) {

 foreach ($employees as $employee) {

?>

 <tr>

 <?

if (! empty($employee)) {

 foreach ($employee as $employeeData) {

 ?>

<td><?php echo $employeeData;?></td>

<?

}

}

?>

</tr>

<?

 }

}

 }

}

?>

</table>


查看完整回答
反对 回复 2021-05-14
  • 1 回答
  • 0 关注
  • 169 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信