dtyqflrr775518 2014-10-08 17:34 采纳率: 100%
浏览 43
已采纳

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

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>'. "

";

}
  • 写回答

1条回答 默认 最新

  • dongrong9938 2014-10-08 17:59
    关注
    $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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度