I'm trying to sort a list alphabetically, this list is taken from a MySQL Database but I can only arrange the information in ascending or descending order. This is the code I'm currently using:
if(isset($_GET['cat_id']))
{
$cat_id=$_GET['cat_id'];
$query="SELECT sub_cat_id,sub_cat_name FROM tbl_sub_category WHERE cat_id='". $cat_id."' ORDER BY tbl_sub_category.sub_cat_id DESC LIMIT $number_of_posts";
}
else if(isset($_GET['sub_cat_id']))
{
$sub_cat_id=$_GET['sub_cat_id'];
$query="SELECT * FROM tbl_directory WHERE d_subcat_id='". $sub_cat_id."' ORDER BY tbl_directory.d_id DESC LIMIT $number_of_posts";
}
else if(isset($_GET['sub_sub_cat_id']))
{
$sub_sub_cat_id=$_GET['sub_sub_cat_id'];
}
else if(isset($_GET['directory_id']))
{
$directory_id=$_GET['directory_id'];
$query="SELECT * FROM tbl_directory WHERE d_id='". $directory_id."'";
}
else
{
$query="SELECT cid,category_name,category_image FROM tbl_category ORDER BY tbl_category.cid DESC LIMIT $number_of_posts";
}
You need to sort by name, not by id.
Note: Put the appropriate field names where necessary.
Corrected code:
<?php
if(isset($_GET['cat_id'])) {
$cat_id=$_GET['cat_id'];
$query="SELECT sub_cat_id,sub_cat_name FROM tbl_sub_category WHERE cat_id='". $cat_id."' ORDER BY tbl_sub_category.sub_cat_name DESC LIMIT $number_of_posts";
}
else if(isset($_GET['sub_cat_id'])) {
$sub_cat_id=$_GET['sub_cat_id'];
$query="SELECT * FROM tbl_directory WHERE d_subcat_id='". $sub_cat_id."' ORDER BY tbl_directory.d_NAME_FIELD DESC LIMIT $number_of_posts";
}
else if(isset($_GET['sub_sub_cat_id'])) {
$sub_sub_cat_id=$_GET['sub_sub_cat_id'];
}
else if(isset($_GET['directory_id'])) {
$directory_id=$_GET['directory_id'];
$query="SELECT * FROM tbl_directory WHERE d_id='". $directory_id."'";
}
else {
$query="SELECT cid,category_name,category_image FROM tbl_category ORDER BY tbl_category.category)name DESC LIMIT $number_of_posts";
}
Got it to work thanks to usermesam0023 just changed this line ORDER BY tbl_directory.directory_name
instead of directory_id
.