Jump to content

Archived

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

DarkHorizon

Trouble sorting numbers in a mysql database *Resolved*

Recommended Posts

I have a database containing products, each has a price.

i want to be able to sort the database by price, high to low or low to high.


$query = "SELECT * FROM $table order by price asc LIMIT $limitvalue, $limit";

$limitvalue and $limit are pagination variables which work okay

the table gets sorted by price okay but i get:

1.50
17.00
19.50
2.00
22.00
234.00
3.19

you can see whats happening, wbut why? and how can i fix it display the correct ascending order.

Share this post


Link to post
Share on other sites
Why does it sort like that? Because you are storing the prices as strings not numbers and that sort is correct for strings. Change your field type to something like FLOAT(6,2) instead of VARCHAR or whatever you're using.

Share this post


Link to post
Share on other sites
blimey, simple as that eh?

i've been waving my fist at the computer Basil Fawlty style...and swearing..

well, its a good job you is a moderators and i aint.

cheers buddy.

edit: Just tried it, it worked....prob resolved!

Share this post


Link to post
Share on other sites

×

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.