bbc496 bbc496 - 3 months ago 18
PHP Question

dropdown menu sorting with query

im sure this would be a simple fix but im lost as to where to place the sort function in this.

<select class="form-control" name="unit"><option></option>
$query=mysqli_query($db, "SELECT * FROM units") or die(mysqli_error($db));
if(!in_array($row["id"], $units)){?>
<option value="<?php echo $row["id"];?>"><?php echo $row["unitsn"];?></option>

it imports all the data i want just looks to be sorted as old on top new on bottom. i would like it ascending in alphabetic order if possible.

Answer Source

You can add ordering to your MySQL query, like so:

$query=mysqli_query($db, "SELECT * FROM units ORDER BY unitsn DESC") or die(mysqli_error($db));

For fields that are strings in MySQL, the default ordering method is alphabetical.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download