[英]How can I pre-select a dropdown option using previously submitted data from database, MySQLI
I have two tables: orders and companies . 我有两张桌子: 订单和公司 。 Note that both tables has a
company_id
row. 请注意,两个表都有一个
company_id
行。
Orders Table : 订单表 :
order_id | order_name | company_id | .....
Companies Table : 公司表 :
company_id | company_name | .....
I have a page with a form that INSERTS data for an order into the orders table in my database. 我有一个页面,其中包含一个表单,用于将订单的INSERTS数据插入到我的数据库中的订单表中。 Part of the order is selecting a company from the companies table to go with it, so I use a dropdown for this.
部分订单是从公司表中选择一家公司,因此我使用下拉列表。
I also have another page with a form that UPDATES that data (an edit page). 我还有另一个页面,其中包含更新该数据的表单(编辑页面)。 When I click on the button to bring up the edit page, I'd like to have the previously submitted data in the input fields.
当我点击按钮调出编辑页面时,我想在输入字段中输入先前提交的数据。 With text fields, I just use
value="<?php echo $company['whatever']; ?>"
. 对于文本字段,我只使用
value="<?php echo $company['whatever']; ?>"
。 However, with dropdown menu's I can't figure out how to pre-select the correct option. 但是,使用下拉菜单我无法弄清楚如何预先选择正确的选项。
Here is my URL structure: 这是我的网址结构:
http://website.com/orders.php?page=edit&order_id=1
Here is what I have to create my dropdown menu: 这是我创建下拉菜单所需的内容:
$getOrder = mysqli_query($db, "SELECT * FROM orders WHERE order_id = ".$_GET['order_id']);
$getCompany = mysqli_query($db, "SELECT * FROM companies ORDER BY company_name ASC");
....
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
while($company = mysqli_fetch_assoc($getCompany)) { ?>
<option value="<?php echo $company['company_id']; ?>">
<?php echo $company['company_name']; ?>
</option>
<?php }
}
?>
How can I get selected
on the option that's been previously selected? 我该如何
selected
对一个已经预先选择的选项?
If $getOrder
has the order details (including company_id
), and $getCompany
has the company details (including company_id
), then you can compare the two. 如果
$getOrder
有订单详细信息(包括company_id
),而$getCompany
有公司详细信息(包括company_id
),那么你可以比较两者。
If they are equal, echo
out selected
as an attribute in the option
, like so: 如果它们相等,则
selected
echo
out作为option
的属性,如下所示:
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
//Get company ID from Order
$orderID = $getOrder["company_id"];
while($company = mysqli_fetch_assoc($getCompany)) { ?>
<option value="<?php echo $company['company_id']; ?>"
<?php
//Compare and echo `selected` if they are equal
if($orderId==$company["company_id"]) echo "selected";
?>>
<?php echo $company['company_name']; ?>
</option>
<?php }
}
?>
That code can be cleaned up, however: 但是,该代码可以清理:
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
$orderID = $getOrder["company_id"];
while($company = mysqli_fetch_assoc($getCompany)) {
echo "<option value='{$company['company_id']}".($getOrder["company_id"]==$getCompany["company_id"] ? " selected" : null).">{$company['company_name']}</option>";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.