如何使用PHP动态创建HTML表?

时间:2014-10-08 17:34:40

标签: php html

我正在尝试使用PHP for循环动态创建一个简单的HTML表。

该表最终需要像这样循环

<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>

不幸的是,PHP代码没有正确生成代码。它不是生成上面列出的正确语法,而是生成底部代码

<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>

这是我的PHP代码,用于生成表

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 个答案:

答案 0 :(得分:3)

$i+1 % 2 == 0

永远不会是真的(好吧......除非是$i == -1),因为%的优先级高于+,这意味着它与< / p>

$i + (1 % 2)

相同
$i + 1

因此,将其更改为($i + 1) % 2,它应该有效。

编辑:与此部分相同的问题:($ds_count - 1 % 2)