2012-12-27 11 views
0

我有一个表格可以选择并回显列(th)和字段数据(td)的名称。但用户可以添加和删除列。我如何编写更适应用户更改的更灵活的代码?我的意思是能够在不知道所有字段的情况下拥有整个表格。选择并回显所有表格(不管它是什么)

<?php 
$sql = "SELECT * from eee"; 
$result = mysql_query($sql,$con); 
$id = mysql_field_name($result, 0); 
$a = mysql_field_name($result, 1); 
$b = mysql_field_name($result, 2); 
$c = mysql_field_name($result, 3); 
?> 

<tr> 
<th><input class="gris" type="text" name="<?php echo $id ?>" value="<?php echo $id ?>"/></th> 
<th><input class="gris" type="text" name="<?php echo $a ?>" value="<?php echo $a ?>"/></th> 
<th><input class="gris" type="text" name="<?php echo $b ?>" value="<?php echo $b ?>"/></th> 
<th><input class="gris" type="text" name="<?php echo $c ?>" value="<?php echo $c ?>"/></th> 
</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="a" value="<?php echo $row['a']?>"/></td> 
<td><input class="blanc" type="text" name="b" value="<?php echo $row['b']?>"/></td> 
<td><input class="blanc" type="text" name="c" value="<?php echo $row['c']?>"/></td> 
</tr> 

<?php } ?> 

</table> 
+3

[**请注意,不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – PeeHaa

+0

首先,请参阅http://php.net/manual/en/function.mysql-field-name.php。 mysql_ *已被弃用。其次,回答你的问题:你可以简单地遍历字段名:'for($ i = 0,$ field_name = mysql_field_name($ result,$ i);($ field_name = mysql_field_name($ result,$ i))!= = false; ++ $ i){用field_name ...做些什么}' – davak

回答

2

你试图做的是提供一个穷人的ORM。我建议你阅读INFORMATION_SCHEMA。这是一个ANSI标准,可以为您提供有关您的数据库和表格的元信息。您可以从那里选择列名,许多现代RDMS都支持它。

另一种选择是调查Doctrine,因为它会为您提供此功能。

0

假设$rs是一个包含结果集的关联数组的数组,就像您可以从大多数数据库接口获得的数组一样。 [特别是那些不使用mysql_ *功能,因为他们正在很快过时]

<?php 

if(count($rs) == 0) { die('no values returned'); } 

foreach($rs[0] as $key => $value) { 
    printf('<th>%s</th>', $key); 
} 

foreach($rs as $row) { 
    foreach($row as $value) { 
     printf('<td>%s</td>', $value); 
    } 
} 

或者,如果你只是必须继续使用旧摇摇欲坠...功能

<?php 

$row = mysql_fetch_array($result) or die('no values returned'); 

foreach($row as $key => $value) { 
    printf('<th>%s</th>', $key); 
} 

do { 
    foreach($row as $value) { 
     printf('<td>%s</td>', $value); 
    } 
} while($row = mysql_fetch_array($result)) 

两者的那些应该打印出适当列标题的表格,以便为您提供任何大小的结果集。

1

首先,像人们评论过的,你应该使用一个新的mysql库,比如mysqli。

您可以使用mysql_fetch_assoc($ result)来获取关联(column => value)数组。然后你可以循环它。

$result = mysqli_query($query); 

// Make the table headers 
$assoc_data = mysqli_fetch_assoc($result); 
echo "<tr>"; 
foreach ($assoc_data as $column => $data) { 
    echo "<th>$column<th>"; 
} 
echo "</tr>"; 

// Fill in the columns with the data from the DB 
do { 
    foreach($assoc_data as $column => $data) { 
     echo "<td><input name=\"$column\" value=\"$data\"></td>"; 
    } 
} while ($assoc_data = mysqli_fetch_assoc($result)); 

这样,如果DB列更改或重命名或其他任何,您的表将自动适应这些更改。

相关问题