Pro Dev
Pro Dev

Reputation: 684

How to group data by date and display in html table using php

Hello I'am new to programming and I stumble upon on grouping data by date and display them in a table with a date separator.

Here is mysql data: enter image description here

How do I group the data into dates and display it in a html table like the one below. enter image description here

I know how to get the data from database and display them in a html table, however grouping data by date seems completely not easy for me I'm a beginner to php programming. I would appreciate any answers.

Upvotes: 1

Views: 2353

Answers (1)

Pupil
Pupil

Reputation: 23948

Output being shown:

enter image description here Created an array with provided the data and looped over the data.

The key is use array key values.

The main array should have date as a key and append all the employee rows to it.

The array structure should be:

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
                )

        )

)

Code:

<?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>

Upvotes: 1

Related Questions