2012-12-26 27 views
0

如何回顯列的名稱?我能夠選擇和迴應內容(TD),但我不知道如何「加載價值」。我的意思是動態加載MySql數據庫。如何回顯列的名稱

我想要從數據庫eee中的td的相同位置「加載值」。我想顯示字段名稱:id,a,b,c

<table> 

<tr> 
    <th><input class="gris" type="text" name="a" value="load value"/></td> 
    <th><input class="gris" type="text" name="b" value="load value"/></td> 
    <th><input class="gris" type="text" name="c" value="load value"/></td> 
    <th><input class="gris" type="text" name="d" value="load value"/></td> 
</tr> 

<?php 
    $result = mysql_query("SELECT * FROM eee"); 
    while($row = mysql_fetch_array($result)) { 
?> 
<tr> 
    <td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td> 
    <td><input class="blanc" type="text" name="primer" value="<?php echo $row['a']?>"/></td> 
    <td><input class="blanc" type="text" name="segon" value="<?php echo $row['b']?>"/></td> 
    <td><input class="blanc" type="text" name="segon" value="<?php echo $row['c']?>"/></td> 
</tr> 

<?php } ?> 

</table> 
+0

從哪裏得到這個「負載值」 – Sibu

+0

你想顯示字段的名稱? – GBD

+0

你也可以使用'mysql_field_name()' – GBD

回答

1

我在PHP很新。我努力嘗試所有答案,但沒有奏效。可能是因爲有一些我沒有理解的東西。這裏的每個人都比我知道得多。試了很多東西后,我檢查了這個工作:

$result = mysqli_query($con, 'SELECT * FROM eee'); 
while ($property = mysqli_fetch_field($result)) { 
?> 

<tr> 
<th><input class="gris" type="text" name="<?php echo $property->name ?>" value="<?php echo $property->name ?>"/></th> 
</tr> 
1

用於$ row數組上的每個值以獲取鍵/值對。密鑰應包含表格中的列名稱。請注意,mysql_fetch_array的默認值會返回一個同時具有關聯AND索引值的數組,只是在遇到某種意外問題時。但我90%肯定所有你需要做的就是用$鍵=> $值組合一個foreach如下所示:

http://us3.php.net/manual/en/control-structures.foreach.php

這裏有mysql_fetch_array頁: http://php.net/manual/en/function.mysql-fetch-array.php

更多明確的解釋:

foreach ($row as $key => $value) { 
    /* echo your table row with the $key and $value here. */ 
} 
+0

還有一個mysql_fetch_assoc() –

1

你可以describe該表在另一個查詢的代價。

<tr> 
<?php 
    $result = mysql_query("DESCRIBE eee"); 
    $inputName = "a"; 
    while($row = mysql_fetch_array($result)) { 
?> 
    <th> 
     <input class="gris" 
       type="text" 
       name="<?php echo inputName; ?>"  <!-- Generates unique names for the inputs too --> 
       value="<?php echo $row['name']; ?>"/> 
    </th> 
<?php $inputName++; } ?> 

</tr> 
+1

爲什麼我們不能使用'mysql_field_name()'?使用'DESCRIBE'的任何具體原因? – GBD

+0

只是因爲我不知道。這正是我過去所使用的。請發佈更好的答案。 –

+0

如果我複製粘貼到我的代碼,它不起作用。可能我不太瞭解它。你能解釋一下嗎,還是請它適應我的問題? – Nrc

0

只寫簡單的列名如u已經知道的名字..

<?php 
$result1 = mysql_query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA WHERE TABLE_NAME ='eee'"); 
while($row = mysql_fetch_array($result1)) { 
?> 




<th><input class="gris" type="text" name="<?php echo $row['COLUMN_NAME ']?>" value="<?php echo $row['COLUMN_NAME ']?>"/></th> 

<?php } ?> 
</tr> 

<?php 
$result = mysql_query("SELECT * FROM eee"); 
while($row = mysql_fetch_array($result)) { 
?> 
<tr> 
<td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td> 
<td><input class="blanc" type="text" name="primer" value="<?php echo $row['primer']?>"/>   </td> 
<td><input class="blanc" type="text" name="segon" value="<?php echo $row['segon']?>"/></td> 
<td><input class="blanc" type="text" name="segon" value="<?php echo $row['tercer']?>"/>  </td> 
</tr> 

<?php } ?> 

</table> 
+0

這就是問題:我不知道我想要動態加載字段名稱的名稱 – Nrc

+0

@Nrc你知道名稱,你正在使用它們來獲取值! –

+0

我簡化了這個問題只是爲了解釋。將來用戶將不得不添加新的列,我不會知道用戶將選擇的名稱。 – Nrc

相關問題