是否可以使用从 PHP 中的 SQL 中提取的数据将默认选择添加到 for 循环中生成的下拉菜单?
Is there a to add a default selection to a dropdown menu generated in a for loop using data pulled from SQL in PHP?
我正在编写一个允许用户更新客户信息的程序。可以更新的字段之一是客户来自的国家/地区。可以使用下拉菜单更新国家/地区,该菜单从 SQL 数据库中提取所有国家/地区名称,并使用 for 循环动态生成。但是,我还想将下拉菜单编程为自动 select 客户所在的国家/地区。有没有办法在保持 for 循环的同时做到这一点?
<label>Country:</label>
<select name="countryCode">
<?php foreach ($countries as $country) : ?>
<option value="<?php echo $country['countryCode']; ?>"><?php echo $country['countryName']; ?></option>
<?php endforeach; ?>
</select>
从数据库中提取客户数据的代码。
$customer_id = filter_input(INPUT_POST, 'customerID');
if ($customer_id == NULL || $customer_id == FALSE) {
$error = "Missing or incorrect customer ID.";
include('../errors/error.php');
} else {
$customers = select_customer($customer_id);
$countries = get_countries();
include('customer_update.php');
}
}
function select_customer($customer_id) {
global $db;
$query = 'SELECT * FROM customers
WHERE customerID = :customer_id';
$statement = $db->prepare($query);
$statement->bindValue(':customer_id', $customer_id);
$statement->execute();
$select = $statement->fetch();
$statement->closeCursor();
return $select;
}
拉取国家数据的代码。
function get_countries() {
global $db;
$query = 'SELECT * FROM countries
ORDER BY countryName';
$statement = $db->prepare($query);
$statement->execute();
$countries = $statement->fetchAll();
$statement->closeCursor();
return $countries;
}
您应该根据数据将 selected
标记添加到选项。可能是这样的:
<label>Country:</label>
<select name="countryCode">
<?php foreach ($countries as $country) : ?>
<option value="<?php echo $country['countryCode']; ?>" <?=($country['countryCode'] == $customers['country_code']) ? 'selected' : '' ?>><?php echo $country['countryName']; ?></option>
<?php endforeach; ?>
</select>
我在这里假设您的客户数据($customers
变量)具有 country_code
字段。
希望你已经明白了。
PS:<?=
语句是 <?php echo
.
的一种快捷方式
我设法解决了。代码 <option value="<?php echo $country['countryCode']; ?>"<?=($country['countryCode'] == $customers['countryCode']) ? 'selected' : '' ?>><?php echo $country['countryName']; ?></option>
似乎可以解决问题。我确实注意到,当我刷新页面时,下拉菜单代码中的更改不适用,但如果我手动返回页面,则会应用。
我正在编写一个允许用户更新客户信息的程序。可以更新的字段之一是客户来自的国家/地区。可以使用下拉菜单更新国家/地区,该菜单从 SQL 数据库中提取所有国家/地区名称,并使用 for 循环动态生成。但是,我还想将下拉菜单编程为自动 select 客户所在的国家/地区。有没有办法在保持 for 循环的同时做到这一点?
<label>Country:</label>
<select name="countryCode">
<?php foreach ($countries as $country) : ?>
<option value="<?php echo $country['countryCode']; ?>"><?php echo $country['countryName']; ?></option>
<?php endforeach; ?>
</select>
从数据库中提取客户数据的代码。
$customer_id = filter_input(INPUT_POST, 'customerID');
if ($customer_id == NULL || $customer_id == FALSE) {
$error = "Missing or incorrect customer ID.";
include('../errors/error.php');
} else {
$customers = select_customer($customer_id);
$countries = get_countries();
include('customer_update.php');
}
}
function select_customer($customer_id) {
global $db;
$query = 'SELECT * FROM customers
WHERE customerID = :customer_id';
$statement = $db->prepare($query);
$statement->bindValue(':customer_id', $customer_id);
$statement->execute();
$select = $statement->fetch();
$statement->closeCursor();
return $select;
}
拉取国家数据的代码。
function get_countries() {
global $db;
$query = 'SELECT * FROM countries
ORDER BY countryName';
$statement = $db->prepare($query);
$statement->execute();
$countries = $statement->fetchAll();
$statement->closeCursor();
return $countries;
}
您应该根据数据将 selected
标记添加到选项。可能是这样的:
<label>Country:</label>
<select name="countryCode">
<?php foreach ($countries as $country) : ?>
<option value="<?php echo $country['countryCode']; ?>" <?=($country['countryCode'] == $customers['country_code']) ? 'selected' : '' ?>><?php echo $country['countryName']; ?></option>
<?php endforeach; ?>
</select>
我在这里假设您的客户数据($customers
变量)具有 country_code
字段。
希望你已经明白了。
PS:<?=
语句是 <?php echo
.
我设法解决了。代码 <option value="<?php echo $country['countryCode']; ?>"<?=($country['countryCode'] == $customers['countryCode']) ? 'selected' : '' ?>><?php echo $country['countryName']; ?></option>
似乎可以解决问题。我确实注意到,当我刷新页面时,下拉菜单代码中的更改不适用,但如果我手动返回页面,则会应用。