I have a PHP array which has several different fields. For some of the rows id and name is the same and the rest of data is different.
I want to display the data in an HTML table without listing the duplicated data more than once. I realize that I can use colspan/rowspan to merge the table cells, but I'm not sure how to do counts/sums based on the matching values.
Below is how my table looks like :
Id Name value1 value2 value3
2312 ABC test test2 test3
2312 ABC XYz 122 211
2455 XYZ val11 val2 val3
Any help will be appreciated.
It can be done by using the rowspan and colspan attribute in HTML. The rowspan is used to merge or combine the number of cells in a row whereas the colspan is used to merge column cells in a table.
To merge table columns in HTML use the colspan attribute in <td> tag. With this, merge cells with each other. For example, if your table is having 4 rows and 4 columns, then with colspan attribute, you can easily merge 2 or even 3 of the table cells.
Merge cellsIn the table, drag the pointer across the cells that you want to merge. Click the Layout tab. In the Merge group, click Merge Cells.
The point is that use a 2-dimension array to store the size(rowspan) value of each cell, and if we don't need a cell, the size of it is zero.
And here's a sample code. You may need to sort your array first.
<?php
$rows = array(
array(2312, 'ABC', 'asda', 'sdf', 'dfg'),
array(2312, 'ABCD', 'asd', 'fgd', 'ret'),
array(2313, 'ABCD', 'asd', 'fgd', 'ret'),
array(2455, 'XYZ', 'sdgg', 'rew', 'gdg'),
);
function try_merge_col( &$rows, &$trs, &$tr, $row_i, $col_i )
{
$row = $rows[$row_i];
$old = $rows[$row_i-1];
if( $row[$col_i] == $old[$col_i] )
{
$tr[$col_i]=0; //throw it
for( $k = $row_i-1; $k>=0; $k-- ) //from down to up
{
if( $trs[$k][$col_i] > 0 ) { //do merge
$trs[$k][$col_i]++;
break;
}
}
}
}
function get_table( $rows )
{
$ret = "";
$trs = array(
array(1,1,1,1,1)
); //used to store cell size
$last_row = null;
for( $i = 1; $i < count($rows); $i++ )
{
$this_tr = array(1,1,1,1,1);
try_merge_col( $rows, $trs, $this_tr, $i, 0 ); //id
try_merge_col( $rows, $trs, $this_tr, $i, 1 ); //name
array_push( $trs, $this_tr );
}
for( $i = 0; $i < count($rows); $i++ )
{
$tr = $trs[$i];
$ret .= "<tr>";
for( $j=0 ; $j<5 ; $j++ )
{
if( $tr[$j] >= 1 )
{
if( $tr[$j] > 1 ) $ret .= "<td rowspan=\"".$tr[$j]."\">";
else $ret .= "<td>";
$ret .= $rows[$i][$j];
$ret .= "</td>";
}
}
$ret .= "</tr>";
}
return $ret;
}
echo get_table( $rows );
?>
I can suggest you to create a new multidimensional array and merge the arrays that have the same ID ... the rest will be easy. Take a look at the example I've made
<?
$init_array = array(
array('id'=>2312, 'name'=>'ABC', 'value1'=>'asda', 'value2'=>'sdf', 'value3'=>'dfg'),
array('id'=>2312, 'name'=>'ABC', 'value1'=>'asd', 'value2'=>'fgd', 'value3'=>'ret'),
array('id'=>2455, 'name'=>'XYZ', 'value1'=>'sdgg', 'value2'=>'rew', 'value3'=>'gdg'),
);
$formatted_array = array();
foreach( $init_array as $element ) {
$formatted_array[ $element['id'] ][] = $element;
}
?>
<table border="2">
<? foreach($formatted_array as $row ): ?>
<tr>
<td rowspan="<?=count($row)?>"><?=$row[0]['id']?></td>
<td rowspan="<?=count($row)?>"><?=$row[0]['name']?></td>
<? foreach( $row as $value ): ?>
<td><?=$value['value1']?></td>
<td><?=$value['value2']?></td>
<td><?=$value['value3']?></td>
</tr><tr>
<? endforeach; ?>
</tr>
<? endforeach; ?>
</table>
the example in CodePad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With