1

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.'>'. "\n";
    $this->output .= '<tbody>'. "\n";
    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>'. "\n";

        //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>'. "\n";

        /********* 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>'."\n".
                             '<td class="table-cell-middle">' . $this->generateTextField($f['field_id'], $f['css_class'], $f['is_editable'], $f['placeholder']) .'</td>'. "\n";

        if($f['field_type'] == 'TEXTAREA')
            $this->output .= '<td class="table-cell-right table-cell-middle">'. $f['display_label'].'</td>'."\n".
                             '<td class="table-cell-middle">' .$this->generateTextAreaField($f['field_id'], $f['css_class'], $f['is_editable'], $f['rows']).'</td>' . "\n";


        //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>'. "\n";


        /********* END DISPLAY BLOCKS *********/

        //IF THIS RUN IS EVEN THEN CLOSE THE ROW
        if( $i+1 % 2 == 0 || $i == $ds_count - 1 )
            $this->output .= '</tr>'. "\n";
    }
    $this->output .= '</tbody>'. "\n";
    $this->output .= '</table>'. "\n\n";

}

1 Answer 1

3
$i+1 % 2 == 0

will never be true (well... except if $i == -1), because % has higher precedence than + in the order of operations, which means this is the same as

$i + (1 % 2)

which is the same as

$i + 1

So change it to ($i + 1) % 2 and it should work.

Edit: same issue with this part: ($ds_count - 1 % 2)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.