Jump to content

[SOLVED] How to get a PROPER sort with PHP


PHPNewbie55

Recommended Posts

I don't know if there is a work around for this but I am having a problem sorting records correctly based on PRICE.

 

It seems that PHP/MySQL does not sort the records correctly when they are being sorted by a NUMBER...

 

Example:

 

I sort my data by PRICE -- ASCENDING...

I get::

10.17

10.98

10.99

108.49

110.98

99.98

98.49

97.50

 

It seems that it just goes by the first number and that's it...

All 1's get put together no matter if it's 100 or 1000..

 

It happens in PHPmyADMIN too... so it seems to be a BUG in PHP...

 

Any way to get PHP to actually sort the data correctly..??

 

Link to comment
https://forums.phpfreaks.com/topic/82057-solved-how-to-get-a-proper-sort-with-php/
Share on other sites

What is the type of the field where the numbers are stored? If you are storing the numbers in a text field, then the numbers are sorted as ASCII, not  numerical. Change the type to float and the sort will work.

 

Ken

Cool... I'll try that.. either way I have some code to rewrite...

 

The default LENGTH/VALUES for DECIMAL is 10,0 so it auto rounds the number up...

If you set the LENGTH/VALUES to 10,2 it gives the decimal points without rounding.

 

 

Thanks... just asking this one question I have learned a couple of things I didn't know before.....!!!

 

VERY MUCH APPRECIATED....

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.