I am using some php to populate a dropdown list from my database. But I cant seem to figure out how to get the sort order of the list alphanumeric.
Here is my code. Its probably a bit of a mess im not a coder im only learning the ropes now, so please take it easy on me.
<label>Category:</label>
<select class="form-control" name="channel_category" id='category_name'
Placeholder="Category" onchange="show_category()">
<?php
$qry="select * from category where parent_cat IS NOT NULL order by
category_id ASC";
$res=mysqli_query($con,$qry) or die(mysqli_error($con));
while ($row = $res->fetch_assoc())
{
?>
<option value="<?php echo $row['category_name'] ?>">
<?php echo $row['category_name'] ?>
</option>
<?php
}
?>
</select>
In the query you're using to get the data for this list, you have explicitly told it to sort the data by category_id
with this:
order by category_id ASC
If you want it to be sorted by category_name
instead, change it to
order by category_name
ASC
is the default sort direction and can be omitted. You can check the MySQL documentation for more details on ORDER BY
.
Change
select * from category where parent_cat IS NOT NULL order by category_id ASC
to
SELECT * FROM category WHERE parent_cat IS NOT NULL ORDER BY category_name ASC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.