根据值

时间:2018-11-04 21:49:57

标签: php mysql loops select while-loop

我有两个表,一个 Workorder 表,其中有一个名为 status_id 的列,它可以让我知道订单的状态(如果其保持原状,进行中或完成。它只是保存状态的数值。

workorder table

我还有一个名为 Status 的表,该表具有匹配的 status_id 和状态文本。

status table

我有一个下拉的while循环,可以拉出Status表并将其回显。

我试图使其选择与工作单表状态相关的下拉选项。

<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代码,因为它等同于回显选定的html标签

我正在尝试将两个表合并为一个

$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"':'';

我无法弄清楚是否有人可以帮助我。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试此操作,获取数组中的状态以显示select并将其与工作订单的status_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>
}

答案 1 :(得分:0)

因为您要加入val,所以两个值在ON w.status_id = s.status_id中将是相同的(而且,您也不会使用别名列SELECT *, w.status_id as wid, s.status_id as sid)。

通过粉碎分配和比较逻辑来看看您造成的麻烦:

代码:(Demo

$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);
}

您应该只使用此命令:Demo

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

输出:

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