0

所以我有一个表格可以编辑我的数据库中已经存在的单位。有一个多选下拉菜单,我可以在其中选择单位的供应商。我需要已经分配给单位的供应商在下拉列表中显示为选中(突出显示),因此用户只需 ctrl+单击他们想要添加到单位的新供应商。

units_suppliers 是我设置的连接表,其中包含成对的单元和供应商 ID,以确定将哪个供应商分配给哪个单元,反之亦然。

这是我目前拥有的代码,但它不起作用。我根据不同的 SQL 查询创建了两个不同的数组,并尝试比较它们是否具有相同的供应商名称,如果是,则将“选定”属性添加到标记中。但这似乎不起作用。谁能帮我吗?

编辑:这两个查询似乎都产生了正确的结果,所以我认为问题出在我的嵌套循环中,但我不知道出了什么问题。感谢您的任何建议。

<td><select size=7 multiple="multiple" name="unitsuppliers[]">
<?php
$sql = "SELECT suppliers_id, supplier_name FROM suppliers GROUP BY supplier_name ORDER BY supplier_name";
$result = mysql_query($sql,$connection);

/* THIS PART FINDS OUT IF THE SUPPLIER SHOULD ALREADY BE HIGHLIGHTED IN THE LIST */
$sql2 = "SELECT suppliers.supplier_name 
            FROM suppliers, units_suppliers 
            WHERE units_suppliers.unit_user_id = ";
if(isset($old_details['unit_user_id'])){
    $sql2 .= " {$old_details['unit_user_id']} ";
} elseif(isset($_POST['old_unit_id'])){
    $sql2 .= " {$_POST['old_unit_id']} ";
} else {
    $sql2 .= " 0 ";
}
$sql2 .=    " AND suppliers.suppliers_id = units_suppliers.suppliers_id";

$result2 = mysql_query($sql2,$connection);

while ($row = mysql_fetch_array($result)) {
            echo "<option value=\"{$row['suppliers_id']}\" ";

            while ($in_junc_table = mysql_fetch_array($result2)) {
                foreach ($in_junc_table as $array){
                    if($array['supplier_name'] == $row['supplier_name']){
                        echo " selected ";
                    }
                }
            }
            echo " >"; 
            echo "{$row['supplier_name']}";                         
        }   
?>
4

1 回答 1

0

请记住,mysql_*现在不推荐使用函数系列,请尝试使用 PDO。

似乎您可以使用JOIN语句并运行单个查询,请尝试以下代码:

$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>','<USERNAME>','PASSWORD');
$statement = $db->prepare("SELECT s.suppliers_id AS id, s.supplier_name AS name, u.unit_user_id AS user_id FROM suppliers s LEFT JOIN units_suppliers u USING suppliers_id WHERE (u.unit_user_id = :user_id OR u.unit_user_id IS NULL) GROUP BY s.supplier_name ORDER BY s.supplier_name");

if (isset($old_details['unit_user_id'])) {
    $user_id = $old_details['unit_user_id'];
}
else if (isset($_POST['old_unit_id'])) {
    $user_id = $_POST['unit_user_id'];
}
else {
    $user_id = 0;
}

$statement->execute(array(
    ':user_id' => $user_id
));

$supliers = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach ($suppliers as $supplier) { ?>
    <option value="<?php echo intval($supplier['id']) ?>" <?php  echo ($supplier['user_id'] == $user_id ? 'selected="selected"' : '') ?>><?php echo htmlspecialchars($supplier['name']) ?></option>
<?php }

没有 PDO:

/**
 * Escape values that will be part of the query
 */
if (isset($old_details['unit_user_id'])) {
    $user_id = intval($old_details['unit_user_id']);
}
else if (isset($_POST['old_unit_id'])) {
    $user_id = intval($_POST['unit_user_id']);
}
else {
    $user_id = 0;
}

$suppliers = array();

$query = "SELECT s.suppliers_id AS id, s.supplier_name AS name, u.unit_user_id AS user_id FROM suppliers s LEFT JOIN units_suppliers u USING suppliers_id WHERE (u.unit_user_id = $user_id OR u.unit_user_id IS NULL) GROUP BY s.supplier_name ORDER BY s.supplier_name";
$result = mysql_query($query,$connection);

if ($result) {
    while ($row = mysql_fetch_assoc($result)) {
        $suppliers []= $row;
    }

    mysql_free_result($result);
}

foreach ($suppliers as $supplier) { ?>
    <option value="<?php echo intval($supplier['id']) ?>" <?php  echo ($supplier['user_id'] == $user_id ? 'selected="selected"' : '') ?>><?php echo htmlspecialchars($supplier['name']) ?></option>
<?php }
于 2013-06-25T18:39:26.407 回答