2014-10-08 82 views
1

我想創建一個簡單的HTML表格動態使用PHP for循環。如何使用PHP動態創建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> 

這是用來生成表

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

} 

回答

3
$i+1 % 2 == 0 

永遠不會爲真(以及我的PHP代碼。 ...除了$i == -1)之外,因爲%的優先級高於+,這意味着這與

相同
$i + (1 % 2) 

這是一樣的

$i + 1 

所以將其更改爲($i + 1) % 2,它應該工作。

編輯:與此部分相同的問題:($ds_count - 1 % 2)

+0

非常感謝:) – Jaylen 2014-10-08 20:02:48