I am trying to create a simple HTML table dynamically using PHP for loop.
The table will finally need to loop like this
<table class="table table-striped">
<tbody><tr><th colspan="4">Training Info</th></tr>
<tr>
<td class="table-cell-right table-cell-middle">Last Training On</td>
<td class="table-cell-middle"><input name="text_1" value="" class="form-control" placeholder="Click here" type="1"></td>
<td class="table-cell-right table-cell-middle">New Field</td>
<td class="table-cell-middle"><input name="text_2" value="" class="form-control" placeholder="Blah Blah" readonly="readonly" type="2"></td>
</tr>
<tr>
<td class="table-cell-right table-cell-middle">More To input</td>
<td class="table-cell-middle"><input name="text_4" value="" class="form-control" placeholder="Start Typing ...." type="4"></td>
<td class="table-cell-right table-cell-middle">Your Opinion</td>
<td class="table-cell-middle"><textarea name="textArea_3" value="" class="form-control" rows="3"></textarea></td>
</tr>
</tbody>
</table>
Unfortunately, the PHP code is not generate the code correctly. Instead of generating the correct syntax listed above, it generate the bottom code
<table class="table table-striped">
<tbody><tr><th colspan="4">Training Info</th></tr>
<tr>
<td class="table-cell-right table-cell-middle">Last Training On</td>
<td class="table-cell-middle"><input name="text_1" value="" class="form-control" placeholder="Click here" type="1"></td>
<td class="table-cell-right table-cell-middle">New Field</td>
<td class="table-cell-middle"><input name="text_2" value="" class="form-control" placeholder="Blah Blah" readonly="readonly" type="2"></td>
<td class="table-cell-right table-cell-middle">More To input</td>
<td class="table-cell-middle"><input name="text_4" value="" class="form-control" placeholder="Start Typing ...." type="4"></td>
<td class="table-cell-right table-cell-middle">Your Opinion</td>
<td class="table-cell-middle"><textarea name="textArea_3" value="" class="form-control" rows="3"></textarea></td>
<td></td><td></td>
</tr>
</tbody>
</table>
This is my PHP code that is used to generate the table
private function generateFields(){
$ds = $this->readSpecs();
$ds_count = count($ds);
$table_class = '';
if(!empty($this->tableClass))
$table_class = ' class="'.$this->tableClass.'" ';
$this->output .= '<table '.$table_class.'>'. "
";
$this->output .= '<tbody>'. "
";
for($i=0; $i< $ds_count; ++$i){
$f = $ds[$i];
//insert new header
if($i == 0 || ($f['block_id'] != $ds[$i-1]['block_id'] ) )
$this->output .= '<tr><th colspan="4">'.$f['block_title'].'</th></tr>'. "
";
//START NEW ROW IN THE TABLE IF THIS LOOP IS THE FIRST ROUND OR RUN IS EVEN
//new row if the
if( $i == 0 || ($i+1 % 2) != 0 )
$this->output .= '<tr>'. "
";
/********* START DISPLAY BLOCKS *********/
if($f['field_type'] == 'TEXT' || $f['field_type'] == 'NUMBER')
$this->output .= '<td class="table-cell-right table-cell-middle">'. $f['display_label'].'</td>'."
".
'<td class="table-cell-middle">' . $this->generateTextField($f['field_id'], $f['css_class'], $f['is_editable'], $f['placeholder']) .'</td>'. "
";
if($f['field_type'] == 'TEXTAREA')
$this->output .= '<td class="table-cell-right table-cell-middle">'. $f['display_label'].'</td>'."
".
'<td class="table-cell-middle">' .$this->generateTextAreaField($f['field_id'], $f['css_class'], $f['is_editable'], $f['rows']).'</td>' . "
";
//FIX HTML TABLE
//add the last 2 cells if the results are odd
if( $i == $ds_count - 1 && ($i+1 % 2) != 0)
$this->output .= '<td></td><td></td>'. "
";
/********* END DISPLAY BLOCKS *********/
//IF THIS RUN IS EVEN THEN CLOSE THE ROW
if( $i+1 % 2 == 0 || $i == $ds_count - 1 )
$this->output .= '</tr>'. "
";
}
$this->output .= '</tbody>'. "
";
$this->output .= '</table>'. "
";
}