简体   繁体   English

从一个表中填充一个下拉菜单,然后从另一个表中设置所选选项

[英]populate a drop down from a table and then set the selected option from another table

I have a table of order statuses that a sales order can have; 我有一张销售订单可以拥有的订单状态表; the statuses are not necessarily sequential. 状态不一定是顺序的。 I can get these statuses into a drop down using the following: 我可以使用以下方法将这些状态放入下拉列表中:

$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC';
$result_order_status = mysql_query($sql1);

echo '<select name="Order_status">';
while ($row = mysql_fetch_array($result_order_status)) {
    echo '<option
    value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
}
echo '</select>';

My problem occurs when I try to fetch the order data from the DB, and then set the dropdown list to that order's status. 当我尝试从数据库中获取订单数据,然后将下拉列表设置为该订单的状态时,会发生我的问题。 I can run the query to get the order status that should be selected, but am not sure how to code how to apply this information. 我可以运行查询来获取应该选择的订单状态,但是不确定如何编写如何应用此信息的代码。 The query to get the order status ID is: 获取订单状态ID的查询为:

SELECT Order_status FROM `mjj_orders` WHERE Order_ID = $_POST['Order_ID']

I have searched (I think comprehensively) and I haven't found a feasible answer or - to be fair - one that I can understand. 我进行了搜索(综合考虑),但没有找到可行的答案,或者-公平地说-我可以理解的答案。

(I have found a question similar to this one, but I am unsure how to get them to elaborate... selected item ind dropdown when editing ). (我发现了一个与此问题类似的问题,但是我不确定如何让他们详细说明... 编辑时选择的项目ind下拉列表 )。

Any and all advice would be great. 任何和所有建议都是很好的。 Thanks. 谢谢。

just drop in some logic checking what status the order you are working on has. 只需加入一些逻辑来检查您正在处理的订单的状态。

$sql = "SELECT Order_status FROM 'mjj_orders' WHERE Order_ID = ".$_POST['Order_ID']; 
$result=mysql_query($sql); 
$myorder=mysql_fetch_array($result);

$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC'; 
$result_order_status = mysql_query($sql1); 

echo '<select name="Order_status">'; 
while ($row = mysql_fetch_array($result_order_status)) { 
    if($myorder['Order_status']==$row['Order_status_ID']){
        $selectCurrent=' selected';
    }else{
        $selectCurrent='';
    }
    echo '<option value="'.$row['Order_status_ID'].'" '.$selectCurrent.'>'.$row['Order_status_name'].'</option>';
} 
echo '</select>'; 

just mark the option where order_status_id = the id that should be selected with selected . 只需将选项标记为order_status_id =应该与selected一起selected的id。 eg. 例如。 the option that will be selected will look like <option selected value="... like this: 将要选择的选项将如下所示: <option selected value="...如下所示:

$id_to_select = // Your logic to get the id to select

$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC';
$result_order_status = mysql_query($sql1);

echo '<select name="Order_status">';
while ($row = mysql_fetch_array($result_order_status)) {
  if($id_to_select == $row['Order_status_ID'])
    echo '<option selected value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
  else     
    echo '<option value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
}
echo '</select>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM