Jeesh, I just spent about 20 minutes writing a reply....
So, here is a scaled down version of what I was hoping to say...
raghavan20 wrote:hawlejr wrote:
This is all fine and dandy except I need to sort by status Alpha and not numeric. The real example is much more complex so I can't just do a PHP array sort
is the status stored as integer or something else?
what do you actually want to sort??
Is it descending or asceding??
I do not know why I am not able to understand the question...if you are free, then provide an example as you said.
The field is stored Numeric in the table. However the output is in the array.
The corresponding status array looks something like this:
Code: Select all
$a_status = array(
1='Active',
2='Inactive',
3='Removed',
4='Not Verified',
5='Hibernation');
and my data looks like this
Code: Select all
ID | Name | Status
1 | Joe | 3
2 | Dave | 5
3 | James | 1
Prior to me taking over this project. This is how they displayed the status
Code: Select all
$qry = "SELECT id,name,status from SOMETABLE ORDER BY status asc";
#get data into and array...
...
..
.
echo $row['id'] . '-';
echo $row['name'] . '-';
echo $a_status[$row['status']] . '<br />';
If I were to sort by status using the above example I would get:
3 - James - Active
1 - Joe - Removed
2 - Dave - Hibernation
However, you will notice that the output does not appear to be sorted by status asc. This is because status is stored as an integer.
To conclude, using the function above. Here is what I've done...
Code: Select all
$qryString = returnMySQLSelectCase( 'status',$a_status,'status_label' );
$qry = "SELECT id,name,status, $qryString from SOMETABLE ORDER BY statuslabel asc";
/*
SELECT id,name,status,
CASE status WHEN 1 THEN 'Active' WHEN 2 THEN 'Inactive'
WHEN 3 THEN 'Removed' WHEN 4 THEN 'Not Valid' WHEN 5
THEN 'Hibernation' END AS status_label
from SOMETABLE ORDER BY statuslabel asc
*/
Which would give me my desired output of:
Code: Select all
3 - James - Active
2 - Dave - Hibernation
1 - Joe - Removed