How do I list data with the same date in one row of the table

Hello,


How do I write the data of the same date registered in the sql database into one row of the table
I showed the example in the picture

My purpose here is I want to show each transaction in one line in audit trail data

I want to show “personid” one for each transaction
I want to show “done_by” one for each transaction
I want to show “done_at” one for each transaction

I’ll do it for fifty pounds.

You must allow for the case of more than one data item by different users in a single second. For example, in your sample data, row id 4 and 5 could have occurred in the same second.

To produce the result you have stated, you would index/pivot the data when you retrieve it, using array indexes for personid, done_at, then done_by. To produce the output from that indexed/pivoted data, you would use four nested foreach() loops.

// array to hold the indexed/pivoted data
$data = [];

// assuming you are using the PDO extension, fetch the data, indexing/pivoting it using the pseronid, done_at, then done_by values
foreach($stmt as $row)
{
	$data[ $row['personid'] ][ $row['done_at'] ][ $row['done_by'] ][] = $row;
}


// to produce the output -
foreach($data as $personid=>$arr1)
{
	foreach($arr1 as $done_at=>$arr2)
	{
		foreach($arr2 as $done_by=>$arr3)
		{
			// at this point, you have a $personid, $done_at, and $done_by value.
			// count($arr3) will tell you how many rows there are going to be in the output section, that can be used for the rowspan attribute in the personid, done_by, and done_at columns.
			foreach($arr3 as $row)
			{
				
			}
		}
	}
}

The markup you are trying to produce would look like (only the first 2 sections shown) -

<table>
  <tr>
    <th>person id</th>
    <th>column name</th>
    <th>old value</th>
    <th>new value</th>
    <th>done by</th>
    <th>done at</th>
  </tr>
  <tr>
    <td rowspan="3">1</td>
    <td>fn</td>
    <td>null</td>
    <td>new fn</td>
    <td rowspan="3">by1</td>
    <td rowspan="3">at1</td>
  </tr>
  <tr>
	 <td>ln</td>
     <td>null</td>
     <td>new ln</td>
  </tr>
  <tr>
	<td>age</td>
	<td>null</td>
	<td>37</td>
  </tr>
  
    <tr>
    <td rowspan="1">1</td>
    <td>age</td>
    <td>37</td>
    <td>20</td>
    <td rowspan="1">by2</td>
    <td rowspan="1">at2</td>
  </tr>

</table>
1 Like

Thank you for the answer
I will try to do as you say

Sponsor our Newsletter | Privacy Policy | Terms of Service