我已经使用数据库中存储的信息创建了该表,但是现在我必须创建一个带有值的选择框,以便仅显示名称。我应该使用if语句吗?
<?php
try {
$con = new PDO('mysql:host=localhost;dbname=snm', "root", "");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT ID,Voornaam,Achternaam,Woonplaats,Postcode,Email,Social,Soort,Categoriebord,Categoriegame FROM gebruiker";
print "<table>";
$result = $con->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
print " <tr>";
foreach ($row as $field => $value) {
print " <th>$field</th>";
}
print " </tr> ";
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach ($data as $row) {
print " <tr> ";
foreach ($row as $name => $value) {
print " <td>$value</td> ";
}
print " </tr> ";
}
print "</table> ";
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
答案 0 :(得分:0)
我对您的代码进行了一些修改,添加了几种方法,分别为您提供属性和带有值的属性
此方法将返回具有属性的值
vmax
此方法只会为您提供属性
function getValues($con, $query)
{
....
}
它将返回如下属性:
function getKeys($con, $query)
{
...
}
您可以为属性Array
(
[0] => ID
[1] => Voornaam
[2] => Achternaam
[3] => Woonplaats
[4] => Postcode
[5] => Email
[6] => Social
[7] => Soort
[8] => Categoriebord
[9] => Categoriegame
)
显示索引为<option value="$row[0]">$row[0]</option>
的属性
我希望这会对您有所帮助,只需将您的代码替换为此
ID