简体   繁体   中英

Php create table from dynamic td tr

I have tried to create a table from the dynamic array with dynamic row cell.

When row found in the array it will add <tr> tag in first and till the next row found in the array. I want like when row found tr tag will start with td tag inside until next row found in array And then again same tr tag will start until the next row found in the array.

Here is the example of an array

$cars = array (
      array(
          'table_body_element'=>'row',
          'cell_text'=>'',
      ),
      array(
          'table_body_element'=>'cell',
          'cell_text'=>'Column 1',
      ),
      array(
          'table_body_element'=>'cell',
          'cell_text'=>'Column 2',
      ),
      array(
          'table_body_element'=>'cell',
          'cell_text'=>'Column 3',
      ),
      array(
          'table_body_element'=>'row',
          'cell_text'=>'',
      ),
      array(
          'table_body_element'=>'cell',
          'cell_text'=>'Column 11',
      ),
      array(
          'table_body_element'=>'cell',
          'cell_text'=>'Column 22',
      ),
      array(
          'table_body_element'=>'row',
          'cell_text'=>'',
      ),
    );

And I have tried to check is it row tag or not and add tr tag on it.

Here is my foreach loop.

echo '<table><thead><tr>';
foreach ($cars as $item){
    echo '<td>'.$item[ 'cell_text' ].'</td>';
    if( $item[ 'table_body_element' ] == 'row' ){
        echo '</tr><tr>';
    }
}
echo '</tr></tbody></table>';

Output

在此处输入图片说明

The output showing like this now. With fast empty tr div without td content and last another tr div printing without content. and I want to remove this only content tr tag and td need to be print in the result.

It's better to have the foreach inside the table, having at the end of the foreach is not good in my opinion. Try this:

echo '<table><tbody>';
foreach ($cars as $item){
    echo '<tr><td>'.$item[ 'cell_text' ].'</td>';
    if( $item[ 'table_body_element' ] == 'row' ){
        echo '</tr>';
    }
}
echo '</tbody></table>';

The problem is that you are always putting out the cell text first, even for row s, and you always open the next line when you get a new row in...

echo '</tr><tr>';

at least you close it, but it's difficult (without maintaining various flags) to decide when to output the start tag.

Alternative, this builds up each row and then uses implode() the the $rows array to add the <tr></tr> tags round them...

echo '<table><thead>';
$rows = [];
$row = '';
foreach ( $cars as $car )   {
    if ( $car['table_body_element'] == 'row' ) {
        if ( !empty($row) ) {
            $rows [] = $row;
            $row = '';
        }
    }
    else    {
        $row .= '<td>'.$car[ 'cell_text' ].'</td>';
    }
}
echo '<tr>'.implode( '</tr><tr>', $rows ).'</tr>';
echo '</tbody></table>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM