Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I show each item in a multi-select dropdown as already selected if it is in the database?
    primarykey
    data
    text
    <p>So I have a form that edits a Units already in my database. There is a multi-select dropdown where I can choose Suppliers to the Unit. I need the suppliers that are already assigned to the unit to come up as selected(highlighted) in the dropdown, so the user only has to ctrl+click new Suppliers they want to add to the Unit.</p> <p>units_suppliers is the junction table I have set up that contains paired Unit and Supplier IDs to determine which supplier is assigned to which unit, and vice-versa.</p> <p>This is the code I currently have, but it's not working. I created two different arrays based on different SQL queries and tried to compare whether they have the same Supplier name, and if so the "selected" attribute was added to the tag. But it doesn't seem to be working. Can anyone help me out?</p> <p>EDIT: The queries both seem to produce the right results, so I think the problem is in my nested loops, but I can't figure out what's wrong. Thanks for any advice.</p> <pre><code>&lt;td&gt;&lt;select size=7 multiple="multiple" name="unitsuppliers[]"&gt; &lt;?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 "&lt;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 " &gt;"; echo "{$row['supplier_name']}"; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload