根据值在while循环期间预选下拉选项

7bsow1i6  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(397)

我有两个表一个workorder表,其中有一列名为status\u id,它让我知道订单的状态,如果是未处理的、进行中的或完成的。它只是保存状态的数值。

我还有一个名为status的表,它有一个匹配的status\u id和状态文本。

我有一个下拉式while循环,它会将状态表拉出来并将其回声输出。
我试图让它选择与workorder表状态相关联的下拉选项。

<select name="status" class="form-control input-default" required>
<option selected disabled>Status</option>       
<?php
$status_sql = "SELECT * FROM STATUS";   
$result_status = $conn->query($status_sql);
while ($status_row = $result_status->fetch_assoc()) {
$status_id   = $status_row['status_id'];
$status_name = $status_row['status_text'];
//select status based on value in workorder row
$select_status = (1 == 1)? 'selected="selected"':'';
echo '<option value="' . $status_id . '" '.$select_status.'>'.$status_name . '</option>';   
}
?>
</select>

我使用这个php代码作为一个is-equal来回显所选的html标记
我想把两张table合二为一

$sql = "SELECT *, w.status_id as wid, s.status_id as sid 
FROM WORKORDER w 
INNER JOIN STATUS s 
ON w.status_id = s.status_id
";

然后把声明变成这样

$select_status = ( $status_row['sid'] == $status_id  = $status_row['status_id'])? 'selected="selected"':'';

我不知道是否有人能帮我解决这个问题。谢谢您!

bxfogqkk

bxfogqkk1#

因为你要加入 ON w.status_id = s.status_id ,这两个值在 SELECT *, w.status_id as wid, s.status_id as sid (另外,您没有使用别名列 $status_row['wid'] ).
看看你把作业和比较逻辑拼凑在一起给自己带来的麻烦:
代码:(演示)

$status_row['sid'] = $status_row['status_id'] = 3;
foreach (range(1, 5) as $status_id) {
    $select_status = ( $status_row['sid'] == $status_id  = $status_row['status_id'])? 'selected="selected"':'';
    var_dump($select_status);
}

输出:

string(19) "selected="selected""
string(19) "selected="selected""
string(19) "selected="selected""
string(19) "selected="selected""
string(19) "selected="selected""

你应该用这个:demo

$select_status = $status_row['sid'] == $status_id ? 'selected="selected"' : '';

输出:

string(0) ""
string(0) ""
string(19) "selected="selected""
string(0) ""
string(0) ""
drnojrws

drnojrws2#

尝试此操作,获取要显示的数组中的状态选择并比较您的工单的状态id和状态id

$status_sql = "SELECT status_id,status_text FROM STATUS";   // keep this query out of while, else will run n times //
    $result_status = $conn->query($status_sql);
    //$statuses = $result_status->fetch_all(MYSQLI_ASSOC); //requires MySQL Native Driver (mysqlnd)
    $statuses = []; 
    while($status_row = $result_status->fetch_assoc()){
       $statuses[] = $status_row;
    }

    $workorder_sql = "SELECT first_name, status_id FROM wordorder LIMIT 0,30";  // or do a where id= if this is a single workorder page 
    $result_workorder = $conn->query($workorder_sql );
    while ($workorder_row = $result_workorder->fetch_assoc()) {

    //rest of code/logic/form fields
    ?>

    <select name="status" class="form-control input-default" required>
    <option selected disabled>Status</option>       
    <?php
    foreach($statuses as $status) {
    $status_id   = $status['status_id'];
    $status_name = $status['status_text'];
    //select status based on value in workorder row
    $select_status = ($workorder_row['status_id']  == $status['status_id'])? 'selected="selected"':'';
    echo '<option value="' . $status_id . '" '.$select_status.'>'.$status_name . '</option>';   
    }
    ?>
    </select>
}

相关问题