0

我正在尝试根据从数据库中获得的数据创建一个下拉列表,但我无法显示它们。有人可以帮帮我吗?

include '../opdrachten/connectionPDO.php';

function Select()
{

$query =$db->prepare("SELECT naam FROM opdracht4");
$query->execute();

foreach ($query as $row)
{
echo '<option value="'.$row['naam'].'">"'.$row['naam'].'"</option>';    
}
} 

<select name="projectleider">   
<?php Select();  ?>
</select>
4

2 回答 2

2

将 传递给$db函数以使其在范围内。

include '../opdrachten/connectionPDO.php';

function Select($db)
{

$query =$db->prepare("SELECT naam FROM opdracht4");
$query->execute();

foreach ($query as $row)
{
echo '<option value="'.$row['naam'].'">"'.$row['naam'].'"</option>';    
}
} 

<select name="projectleider">   
<?php Select();  ?>
</select>
于 2013-02-12T09:39:57.633 回答
0

您应该从函数返回值。尝试这个,

function Select()
{

  $query =$db->prepare("SELECT naam FROM opdracht4");
  $query->execute();

 $str = '<option value="">Select anyone</option>';
 foreach ($query as $row)
 {
  $str.= '<option value="'.$row['naam'].'">"'.$row['naam'].'"</option>';    
 }
 return $str;
} 

打电话试试这个,

<select name="projectleider">   
<?php echo Select();  ?>
</select>
于 2013-02-12T09:40:54.880 回答