2014-12-03 201 views
0

我試圖在使用PHP查詢數據庫時創建動態html表格。我在理解如何在它附加兩個不同的循環以確保我想要創建的表格正確生成時遇到了一些困難。使用PHP動態創建HTML表格

<table><tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr> 
<tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr> 
<tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr></table> 
<hr> 
<table><tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr> 
<tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr> 
<tr> 
    <td>$ID and $ITEM_NAME</td> 
    <td>$ID and $ITEM_NAME</td> 
</tr></table> 
<hr> 

正如你可以看到我有追加到一個HTML錶行和第3行,我需要追加


並創建一個新的HTML表格,同時繼續我的查詢兩行的信息價值。

目前我的PHP代碼看起來像這樣。

foreach($item_ID as $x => $x_value) { 

     $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'"; 
     $query = $this -> db -> query($sql); 

     foreach($query->result() as $row){ 
      $item_name = $row->item_name; 
     } 

     $html_output .= '<tr><td>'; 
     $html_output .= $x_value. " ".$item_name; 
     $html_output .= '</td>'; 

     //Not sure how to proceed with closing the rows and opening new one on 3rd query item? 

    } 

請注意,我使用Codeigniter,但我只是尋找一些在邏輯上的幫助。

讓我知道,如果我能幫助清理事情。

感謝您花時間閱讀本文。任何幫助將不勝感激。

+0

使用行計數器和模,所以你有'$ html_output。= $ iRowCounter%2 == 0? 「」:「」;'...作爲非常粗略的近似 – CD001 2014-12-03 16:59:37

回答

1

首先,做一個查詢:

$sql = "SELECT id, item_name FROM item_table WHERE id IN ('" . implode("', '", $item_ID) ."')"; 
$query = $this -> db -> query($sql); 

設置一個計數器

$i = 1; 

然後開始你的表的代碼

$html_output .= '<table>'; 

現在遍歷結果,創建行和尋找那個櫃檯

foreach($query->result() as $row){ 
    if ($i % 2 == 1) { 
     $html_output .= '<tr>'; 
    } 
    $html_output .= '<td>' . $row->id .' and' . $row->item_name . '</td>'; 
    if ($i % 2 == 0) { 
     $html_output .= '</tr>'; 
    } 
    if ($i % 6 == 0) { 
     $html_output .= '</table><hr><table>'; 
    } 
    $i++; 
} 
  • $i % 2 == 1部分每個奇數項之前啓動一個新的錶行(1,3,5,......)。

  • $i % 2 == 0部分在每個偶數項目(2,4,6,...)之後結束表格行。

  • $i % 6 == 0部分在每第三行之後添加一行(實際上在每個第六個項目之後,因爲每行有兩個項目在三行之後)。

最後關閉表格,但首先檢查我們是否有偶數個項目。如果不是,請添加一個空的表格單元格。

if ($i % 2 == 1) { 
    $html_output .= '<td></td></tr>'; 
} 
$html_output .= '</table>'; 
+0

嘿保羅,你的查詢有什麼問題嗎?語法似乎關閉。 – BaconJuice 2014-12-03 18:46:05

+0

你是對的,錯過了一個單引號,現在修好了。 – Paul 2014-12-03 18:51:16

+0

我只注意到別的東西。我的要求的一部分是它也顯示每行兩個項目。上面的代碼每行輸出一個項目。再次感謝你。我喜歡這個查詢想法!想知道你是否也可以幫我解決第二個問題? – BaconJuice 2014-12-03 19:14:06

0

要解釋,它聽起來像是你想創建一個新的表與每個奇數行。另外,您希望將表格與HR標籤分開。

$i = 0; //initialize as 0 rows 
foreach($item_ID as $x => $x_value) { 

    $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'"; 
    $query = $this -> db -> query($sql); 

    foreach($query->result() as $row){ 
     $item_name = $row->item_name; 

     $row_output = '<tr><td>'; 
     $row_output .= $x_value. " ".$item_name; 
     $row_output .= '</td>'; 
     //presumably you would close the row here too, unless you have more table cells you aren't showing us 

     $i++;//we have a row, so increment 
     if (!($i % 2)) { // if this has a remainder, then the current row counter is odd 
      if ($i != 1) { 
      //beginning of a table that isn't the first table, add the hr 
      $html_output .= '<hr>'; 
      } 
      $html_output .= '<table>'; // open table 
      $html_output .= $row_output; 
     } else { //this is an even numbered row, the last row of the table 
      $html_output .= $row_output; 
      $html_output .= '</table>'; // close table 
     } 
    } 
} 
+0

您好faerysteel,上面的代碼錯過了我的需求的一部分,我必須在每行​​輸出2項。檢查上面的輸出html示例。再次感謝您的幫助! – BaconJuice 2014-12-03 19:07:39

+0

請說明: 對於每兩個結果,是一行。然後每兩行是一張表。正確? 爲什麼要將行分成多個表?爲什麼要將多個數據塊($ ID和$ ITEM_NAME)放入每個單元格,以及爲什麼每行有兩個?這似乎是濫用表格。 你究竟想要完成什麼?可能有更好的方法。 – faerysteel 2014-12-03 19:29:00