Link to home
Start Free TrialLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

SELECT the column and add 0 to it (or maybe multiply it by 1).  Best wishes, ~Ray
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterdeB

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
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In your php, try to form your query like that:

SELECT * FROM yourtable ORDER BY id DESC

Open in new window


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.
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
 printf( "%05d", $rows['products_id'] 

Open in new window


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.
Thanks heaps