2013-08-28 46 views
0

我有一個網頁(從PHP文件創建),需要顯示一個30行長的表,並允許用戶輸入30行中的每一行的值,然後按按鈕讓php處理他們輸入的內容。簡單的HTML表格代碼使用PHP

無論如何,我們不用寫出一個具有30行的表的普通HTML表單,我不知道他們是否有辦法用PHP創建這個表的代碼少得多。

SO,因爲它是它看起來像

<form name="createtable" action="?page=createtable" method="POST" autocomplete="off"> 
    <table border='1'> 
     <tr> 
      <th> Weight </th> 
      <th> CBM Min </th> 
      <th> CBM Max </th> 
      <th> Min </th> 
     </tr> 
     <tr> 
      <th> 1000 </th> 
      <th> 0.1 </th> 
      <th> 2.3 </th> 
      <th> <input type=text name=min1> </th> 
     </tr> 
     <tr> 
      <th> 1500 </th> 
      <th> 2.31 </th> 
      <th> 3.5 </th> 
      <th> <input type=text name=min2> </th> 
     </tr> 
     <tr> 
      <th> 2000 </th> 
      <th> 3.51 </th> 
      <th> 4.6 </th> 
      <th> <input type=text name=min3> </th> 
     </tr> 
      ..... + 27 more rows 
    </table> 
</form> 

我目前只是寫出像上面的完整表格,體重,煤層氣最小和最大的價值不是以標準稅率增加使正常循環不會工作我猜,這些值可以放入數組嗎?我的php很生鏽

+0

找出代碼的哪個部分在重複(提示:帶孩子的tr),並將該代碼移入循環。因此每行都會打印一個包含內容的tr。 – JimL

+0

只是一個註釋,''是指表頭,在你可能想要的前四個'​​' – Jordan

+1

你從哪裏得到這些值?那些在你的''標籤之間? (順便說一下,不應該是'​​'標籤嗎?通常,您應該只有一組''標籤 - 其餘所有行應使用'​​'標籤。)如果您從中獲得值一個數據庫,請讓我們知道。最好的,如果你也顯示代碼。 – gibberish

回答

2

這是一個可能的解決方案。

/* this should contain all rows, a resultset from a database, 
    or wherever you get the data from */ 
$rows = array(
    array(
     'weight' => 1000, 
     'cbm_min' => 0.1, 
     'cbm_max' => 2.3 
    ), 
    array(
     'weight' => 1500, 
     'cbm_min' => 2.31, 
     'cbm_max' => 3.5 
    ), 
    array(
     'weight' => 2000, 
     'cbm_min' => 3.51, 
     'cbm_max' => 4.6 
    ) 
); 

?> 
<form name="createtable" action="?page=createtable" method="POST" autocomplete="off"> 
    <table border='1'> 
    <tr> 
     <th> Weight </th> 
     <th> CBM Min </th> 
     <th> CBM Max </th> 
     <th> Min </th> 
    </tr> 
<?php 
$i = 1; // I'll use this to increment the input text name 
foreach ($rows as $row) { 
    /* Everything happening inside this foreach will loop for 
    as many records/rows that are in the $rows array. */ 
?> 
    <tr> 
     <th> <?= (float) $row['weight'] ?> </th> 
     <th> <?= (float) $row['cbm_min'] ?> </th> 
     <th> <?= (float) $row['cbm_max'] ?> </th> 
     <th> <input type=text name="min<?= (float) $i ?>"> </th> 
    </tr> 
    <?php 
    $i++; 
} 
?> 
    </table> 
</form> 
<?php 
// Continue executing PHP 
+0

+1對於你快速的回答 –

+0

你不需要'echo'那些PHP語句嗎?或者(浮動)以某種方式照顧它? – Jordan

+0

@Jordan:'<?='是一個簡短的回聲,不用擔心,它不會受到刪除短php標籤的影響,所以它應該是安全的使用。浮動只是爲了確保我們正在打印數字(XSS)。 – JimL

0

你肯定可以做到這一點使用PHP

您將需要一個二維數組將所有這些都是你的所有<tr> * <th>即30 * 3在你的情況下作爲第三要顯示的值列將包含<input>

很容易找到聲明數組和寫for循環的語法。

for($i=0;$i<30;$i++) 
{ 
for($j=0;$j<4;$j++) 
{ 
    echo "<td>array[$i][$j]</td>" 
} 
echo "<input>...</input>" 
}