Combine strings in PHP and HTML

I have a PHP array that has several different fields. For some of the strings, id and name are the same, and the rest of the data is different.

I want to display data in an HTML table without listing duplicate data more than once. I understand that I can use colspan / rowspan to combine table cells, but I'm not sure how to do the calculations / sums based on the corresponding values.

Below is what 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 would be appreciated.

+6
source share
6 answers

I can suggest you create a new multidimensional array and combine arrays with the same ID ... the rest will be easy. Take a look at the example I 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> 

CodePad example

+1
source

The fact is that using a 2-dimensional array to store a rowspan for each cell, and if we do not need a cell, its size is zero.

And here is 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 ); ?> 
+1
source

Sort array.

Keep the identifier and name every time, and then at the beginning of each line, check if the identifier and name coincide with the last. If they are, combine the lines.

0
source

It looks like you might just need to run a for loop with each row and column.

Some kind of crude pseudo code:

 idArray = []; idVal_Dict = {}; for col in table{ // Run through each Column .. if col == 1{ for id in col1{ if ! id == 1{ // If we're on the first column .. idArray.append(id.value()); // append id value .. } } }else{ for val in col{ // For Other columns, find the matching ID & store to a dictionary .. idVal_Dict[val.value()] = idArray[val]; } } 
0
source

Actually, I donโ€™t know that you plan to show different data in the table with the same Id in two rows. But I think that you should combine the values โ€‹โ€‹in your php codes (and not in the view), and then send them for viewing as clear data. This way you can check for equal ID values โ€‹โ€‹and get their sum before merging.

0
source

Use the PHP if else statement to check the number of identifiers, and then use the foreach loop to display the information. I think this should work. You use count as

 count($data[id]); 
-1
source

Source: https://habr.com/ru/post/926085/


All Articles