Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

When ordering an array it only takes into account the first integer

My array thinks that 10.3 is smaller than 3.2

     

php_dave

2:12 pm on Jun 11, 2013 (gmt 0)



This has got to have an extremely simple answer but I have amazingly never come across this problem before until now.

I have an array which contains products and prices.

Product 1 - 3.50
Product 2 - 19.30
Product 3 - 7.25

When I order the array by price ascending (ORDER BY price ASC) it SHOULD obviously be:

Product 1 - 3.50
Product 3 - 7.25
Product 2 - 19.30

However, it brings up the results as:

Product 2 - 19.30
Product 1 - 3.50
Product 3 - 7.25

It seems to only take into account the first integer of each price and orders the array based on that.

Why? Have I really gone so many years doing PHP to have never ordered an array by integers of different lengths before?

Am I missing something really, really obvious? I must be! But I don't even know what to try and look for in Google, which is why I am here.

Thank you to anyone who can help me with this embarrassing problem.

jatar_k

2:31 pm on Jun 11, 2013 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think it's ordering them as strings instead of floats/ints

maybe you need to try typecasting them

php_dave

2:34 pm on Jun 11, 2013 (gmt 0)



Hi Jatar,

Thanks for your reply. I have in fact just worked it out. It was of course very simple.

In the database I had the "price" row type set as INT. I have now changed it to DECIMAL (4,2) and it works fine.

Haha, I blame not having slept for over 24 hours.

jatar_k

2:36 pm on Jun 11, 2013 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



good fix :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month