如何使用PHP动态创建HTML表?
我正在尝试使用PHP for循环动态创建一个简单的HTML表 表最终需要这样循环如何使用PHP动态创建HTML表?,php,html,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</t
<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";
}
专用函数generateFields(){
$ds=$this->readSpecs();
$ds\u count=计数($ds);
$table_class='';
如果(!empty($this->tableClass))
$table_class='classe=“”。$this->tableClass''';
$this->output.=''。\n;
$this->output.=''。\n;
对于($i=0;$i<$ds\u count;++$i){
$f=$ds[$i];
//插入新标题
如果($i==0 | | |($f['block_id']!=$ds[$i-1]['block_id']))
$this->output.=''.$f['block\u title'].'。“\n”;
//如果此循环是第一轮或运行为偶数,则在表中开始新行
//如果
如果($i==0 | |($i+1%2)!=0)
$this->output.=''。\n;
/*********启动显示块*********/
如果($f['field\u type']='TEXT'\124;$f['field\u type']=='NUMBER')
$this->output.=''.$f['display_label'].'。“\n”。
'.$this->generateTextField($f['field\u id'],$f['css\u class'],$f['is\u editable'],$f['placeholder'])。'。“\n”;
如果($f['field_type']=='TEXTAREA')
$this->output.=''.$f['display_label'].'。“\n”。
'.$this->generateTextAreaField($f['field\u id'],$f['css\u class'],$f['is\u editable'],$f['rows'])。'。“\n”;
//修复HTML表
//如果结果为奇数,则添加最后2个单元格
如果($i==$ds_计数-1&($i+1%2)!=0)
$this->output.=''。\n;
/*********结束显示块*********/
//如果此运行为偶数,则关闭该行
如果($i+1%2==0 | |$i==ds|u计数-1)
$this->output.=''。\n;
}
$this->output.=''。\n;
$this->output.=''。\n\n;
}
永远不会为真(除非$i==-1
),因为%
在操作顺序上的优先级高于+
,这意味着这与
$i + (1 % 2)
$i + 1
这和
$i + (1 % 2)
$i + 1
因此,将其更改为($i+1)%2
,它应该可以工作
编辑:此部分存在相同问题:($ds\u count-1%2)