homepage Welcome to WebmasterWorld Guest from 54.204.249.184
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
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




msg:4583117
 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




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

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

maybe you need to try typecasting them

php_dave




msg:4583129
 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




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

good fix :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved