Jump to content


Photo

Trouble sorting numbers in a mysql database *Resolved*


  • Please log in to reply
2 replies to this topic

#1 DarkHorizon

DarkHorizon
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationScotland

Posted 22 June 2006 - 02:42 PM

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.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 22 June 2006 - 02:58 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#3 DarkHorizon

DarkHorizon
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationScotland

Posted 22 June 2006 - 03:49 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users