如何在创建类似的下拉列表时避免重复代码?

时间:2016-04-27 09:30:52

标签: php mysql pdo

下面是我创建下拉列表的代码,我能够从MySQL检索数据,但我必须为每个字段重复选项步骤,如何为下拉列表设置代码,以便我可以缩短在选择列表中调用一次,就像在for循环中一样。如果没有选择,如何创建显示的错误消息,如果我从列表中选择一个数据,它存储在数据库中

 <form action="sign.php" method="post">
 <label for="employee_name">Employee name</label>
 <select name = "employee_name">
 <option value=""> -----------Select----------- </option> 
 <?php
 $stmt = $pdo->prepare('Select name from people');
 $stmt->execute();
 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
 echo '<option>'.$row['name'].'</option>';
 }
 ?>
 </select> 
 <label for="manager">manager</label>
 <select name = "manager">
 <option value=""> -----------Select----------- </option> 
 <?php
 $stmt = $pdo->prepare('Select name from people');
 $stmt->execute();
 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
 echo '<option>'.$row['name'].'</option>';
 }
 ?>
 </select> 
 <label for="senior">Senior</label>
 <select name = "Senior">
 <option value=""> -----------Select----------- </option> 
 <?php
 $stmt = $pdo->prepare('Select name from people');
 $stmt->execute();
 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
 echo '<option>'.$row['name'].'</option>';
 }
 ?>
 </select> 
 <input type="submit" name="register" value=" Click to Add"></button>
 </form>

3 个答案:

答案 0 :(得分:0)

使用数组并运行一次查询来设置数组中的选项。

<?php
$optionArray = array();
 $stmt = $pdo->prepare('Select name from people');
 $stmt->execute();
 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $optionArray[] = '<option value="'.$row['name'].'">'.$row['name'].'</option>';
 }
 ?>

<form action="sign.php" method="post">
 <label for="employee_name">Employee name</label>
 <select name = "employee_name">
 <option value=""> -----------Select----------- </option> 
 <?php
foreach($optionArray as $row)
{
    echo $row;
}
 ?>
 </select>

答案 1 :(得分:0)

  

如何为下拉菜单设置代码,以便我可以缩短在选择列表中调用一次,就像在for循环中一样。

只需创建一个循环。虽然它应该是foreach,而不是。

首先收集您的数据,

$people = $pdo->query('Select name from people order by name')->fetchAll(PDO::FETCH_COLUMN);
$sections = [
    'employee_name' => 'Employee name',
    'manager'       => 'Manager',
    'senior'        => 'Senior',
];

然后将其循环

<form action="sign.php" method="post">
<?php foreach ($sections as $label => $caption): ?>
    <label for="<?=$label?>"><?=$caption?></label>
    <select name = "<?=$label?>">
        <option value=""> -----------Select----------- </option> 
        <?php foreach ($people as $name): ?>
            <option><?=$name?></option>
        <?php endforeach ?>
    </select> 
<?php endforeach ?>
<input type="submit" name="register" value=" Click to Add"></button>
</form>

答案 2 :(得分:-1)

每次都可以创建执行相同任务的方法,而不是重复此操作。

<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option> 
   <?=loadOptions('name','people');?> <!--shortcut way to echo in php-->
</select>

<?php 
function loadOptions($field,$table){
   $stmt = $pdo->prepare('Select '.$field.' from '.$table);
   $stmt->execute();
   $options='';
   while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $options .= '<option>'.$row['name'].'</option>';
   }
   return $options;
}
?>