PeterdeB
asked on
Php sorting mysql results based on id's that have different numbers of digits
Hi
I have a database that needs to be displayed based on their id, in phpmyadmin it works correctly
1
23
156
276
1245
but in php it sorts like
1
1245
156
23
276
I have tried to search for this here but did not solve it, hope anyone can help me, the id's are all digits
could or should I just insert digits into the numbers that are:
1
23
156
like they will become
0001
0023
0156
if so, how should i do that?
I prefer filling up the id's with more digits, the numbers will never become greater than 5 digits but if for display php can do this so i do not have to alter the database that is even safer i think
I have a database that needs to be displayed based on their id, in phpmyadmin it works correctly
1
23
156
276
1245
but in php it sorts like
1
1245
156
23
276
I have tried to search for this here but did not solve it, hope anyone can help me, the id's are all digits
could or should I just insert digits into the numbers that are:
1
23
156
like they will become
0001
0023
0156
if so, how should i do that?
I prefer filling up the id's with more digits, the numbers will never become greater than 5 digits but if for display php can do this so i do not have to alter the database that is even safer i think
SELECT the column and add 0 to it (or maybe multiply it by 1). Best wishes, ~Ray
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it orders correctly in mysql as i have stated, it is defined as int, i have no idea what you mean by your answer with all respect, i did not register here because of my genius in mysql haha anyway thanks ill try
OK, perhaps I had it backwards! To order this information in PHP try natsort().
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In your php, try to form your query like that:
That should do it. I think that's what phpmyadmin is doing also. Just try and let me know what happened. But the experts are right to demand your actual php code to help you better.
SELECT * FROM yourtable ORDER BY id DESC
That should do it. I think that's what phpmyadmin is doing also. Just try and let me know what happened. But the experts are right to demand your actual php code to help you better.
ASKER
hey sorry for the late response while being busy with this project my system does suddenly not execute anything anymore except for a few firefox windows
ASKER
printf( "%05d", $rows['products_id']
using sprintf solved all the issues, thanks all for participating, to your opinion, what should i do with the points, because some of you did actually point me in that direction ofcourse
Choose the answers that helped you and assign points as you see it.
ASKER
Thanks heaps