Forum Moderators: coopster
28
29
3
30
31
If you can't change your datatype and you're not retrieving a huge dataset, then I'd load them into a PHP array and then sort the array. Not the most efficient way to do it, but it works for me... If it was a larger dataset, I'd redo my table.
Burner
PS if it's not MySQL, I apologize...
The fix is to set the datatype of your field to one of the numeric types like DECIMAL, DOUBLE, FLOAT, INT instead of a text type field. then MySQL will sort according to number rules instead of alpha rules.
Or if you don't retrieve large datasets you could just retrieve the data from the MySQL into an array and sort the array. Most easily done for me using the following two functions:
The most efficient always IMHO is always to let the MySQL do it if possible. MySQL data structure makes or breaks a good website.
My 2 cents...
Burner
The reason I set it up as "text" was occassionally there are items that require a text entry instead of the 1-20 number.
Is it possible to use a leading zero on your numbers below 10? I think that will solve the sort problem.
ID (int/primary key)
IDText (varchar, text, whatever you're using)
Now go ahead and insert the numbers 1 through 20 in order where both ID and IDText are the same. Then continue onward with your text-based after that starting with 21. If you think you'll ever use more than 1-20, start your text-based ID's at a higher number like 1,000.
Then modify your original table - replace the TEXT values with the corresponding ID number from the new table, and change the field type to int. From now on when you do a select, you can do a join to the new table to be able to read the value but when you're sorting the data you can just order by the fieldname and now, as a numeric field, everything will be in order.