我从数据库填充组合框。到目前为止一切都还可以,但是当我检查html结果时,组合框中有空选项标签foreach行。表中没有空行。我没有实现解决方案,问题只发生在 get_flight_locations 函数上。
结果
<select class="form-control" name="flightfrom">
<option>Choose please</option>
<option value="1">Baku-GYD</option>
<option></option>
<option value="2">Berlin-TXL</option>
<option></option>
</select>
的index.php
<?php
$lst_to = Database::getInstance()->get_flight_locations();
?>
<select class="form-control" name="flightfrom">
<option><?php echo $lang[$clang]["lblChoose"] ?></option>
<?php
while($row = mysqli_fetch_array($lst_to)) {
printf(
"<option value=\"%s\">%s-%s<option>",
$row["city_id"],
$row["city_name"],
$row["airport_code"]
);
}
?>
</select>
数据库类
<?php
class Database extends mysqli {
private $db = DB_NAME;
private $user = DB_USER;
private $pass = DB_PASS;
private $host = DB_HOST;
private static $instance = null;
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
parent::__construct($this->host, $this->user, $this->pass, $this->db);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
parent::set_charset('utf-8');
}
public function __clone() {
trigger_error('Clone is not allowed', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed', E_USER_ERROR);
}
public function get_flight_locations() {
return $this->query("select * from v_flight_location");
}
}
?>
答案 0 :(得分:0)
您在选项关闭标记中忘记了/
符号。因此,它不会关闭<option>
标记,而是打开另一个标记。
这是一个修复:
printf(
"<option value=\"%s\">%s-%s</option>",
$row["city_id"],
$row["city_name"],
$row["airport_code"]
);