Write data table with more than 2 columns by foreach

4 columns with foreach

$i = 1;
foreach($fields as $key => $field):
	$label = $field[0];
	$class = $key;

	if($i % 4 == 1){
		echo '<tr>';
	}

	echo '<td width="150">' . $label . ':</td>';
	$i++;

	echo '<td width="200"><span class="' . $class . '"></span></td>';
	$i++;

	if($i % 4 == 0){
		echo '</tr>';
	}
endforeach;

 

2 Columns

<table id="itable" class="table">
	<tbody>
		<?php 
		$keys = array_keys($receipt_cols);
		for($i = 0; $i < count($receipt_cols); $i+2){
			echo '<tr>';

			$key = $keys[$i];
			$field = val($receipt_cols, $key);
			echo '<td>' . $field[0] . '</td>';
			echo '<td><span class="' . $key . '"></span></td>';
			$i++;

			$key = $keys[$i];
			$field = val($receipt_cols, $key);
			echo '<td>' . $field[0] . '</td>';
			echo '<td><span class="' . $key . '"></span></td>';
			$i++;

			echo '</tr>';
		}
		?>
	</tbody>
</table>

 

4 columns

<div class="responsive_table" style="overflow-x:auto;">
    <table class="table table-striped" style="min-width: 900px;">
      <thead>
        <tr>
          <th scope="col">名稱</th>
          <th scope="col">值</th>
          <th scope="col">名稱</th>
          <th scope="col">值</th>
          <th scope="col">名稱</th>
          <th scope="col">值</th>
          <th scope="col">名稱</th>
          <th scope="col">值</th>
        </tr>
      </thead>
      <tbody>
        <?php
        for($i = 0; $i < count($all_fields); $i+4){
            echo '<tr>';

            $field = val($all_fields, $i);
            if($field == false){
                echo '<td></td>';
                echo '<td></td>';
            }else{
                echo '<td>' . $field->display . '</td>';
                echo '<td>' . $this->train_plan_all_fields_m->show_field($field) . '</td>';
            }
            $i++;

            $field = val($all_fields, $i);
            if($field == false){
                echo '<td></td>';
                echo '<td></td>';
            }else{
                echo '<td>' . $field->display . '</td>';
                echo '<td>' . $this->train_plan_all_fields_m->show_field($field) . '</td>';
            }
            $i++;

            $field = val($all_fields, $i);
            if($field == false){
                echo '<td></td>';
                echo '<td></td>';
            }else{
                echo '<td>' . $field->display . '</td>';
                echo '<td>' . $this->train_plan_all_fields_m->show_field($field) . '</td>';
            }
            $i++;

            $field = val($all_fields, $i);
            if($field == false){
                echo '<td></td>';
                echo '<td></td>';
            }else{
                echo '<td>' . $field->display . '</td>';
                echo '<td>' . $this->train_plan_all_fields_m->show_field($field) . '</td>';
            }
            $i++;

            echo '</tr>';
        }
        ?>
      </tbody>
    </table>
</div>