Display correct prices from database

I am displaying price values from my database and using the following code to display them.

FORMAT(properties.price,0) as price

So a database value of 11000 shows as 11,000. The problem is that now hourly rates need to show so if the database for instance has a value of 5.8 it needs to display as that whereas currently is shows as 6.

Any ideas how I change the format to achieve this?
BrighteyesDesignAsked:
Who is Participating?
 
mcnuteConnect With a Mentor Commented:
Another question: Do you have some sort of validation when inserting these prices in your database? Do you give some sort of restriction to that field entry or is these field free to be filled with whatever format?

If so, I stronly suggest you to dictate a format to have consistant data in your database. That said, it can even avoid you the trouble of formatting at all. But since you have tow potential types of prices 4.90 and 18,000.00 and it is of type string you could do the following:

$intpounds = explode('.' $row['price']);

if (strlen($intpounds[0]) > 3 ) {   // if its more than hundereds display everything before the dot.
    echo $intpounds[0];
} else { // else display the full price just out of the database 
   echo $row['price'];
}

Open in new window


Hope that helps!
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Change the second argument in your Format statement to the number of decimals you'd like. If you'd prefer that 5.8 be displayed as 5.80, change the statement to
FORMAT(properties.price,2) as price

Open in new window

0
 
BrighteyesDesignAuthor Commented:
Thanks for that, almost there...

Just one thing, if you look at the screenshot...ideally, it should read £4.90 - £18,000 rather than £4.90 - £18,000.00 Is that possible?

result
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Kim WalkerWeb Programmer/TechnicianCommented:
Obviously, if you know that the higher number is ALWAYS in full pounds, you'd use 0 as the second argument for the format statement for that number. But if that's not the case, it would require some sort of if/then clause to recognize when to include cents and when not to. Unfortunately this is beyond the scope of my knowledge of SQL. Perhaps another expert will comment further.
0
 
BrighteyesDesignAuthor Commented:
No worries, thanks for getting this far.

The example in the screenshot is just an example and kind of false as it mixes an hourly rate and salary. It would always be one or the other.

So  ....£4.90 - £5.60    or     £30,000 - £40,000

It's pretty safe to say that there won't be any hourly rates above £100 pound though so I reckon an if statement saying something like "if over 100 don't add .00"? No idea how to this of course! but as you say, if there's any other experts out there that do?
0
 
mcnuteCommented:
It would be really helpful if you show us how the price is being stored in the database. For now we only know about how you want it to be. So tell us how it IS right now to give a formatting solution.
0
 
BrighteyesDesignAuthor Commented:
Sure, it was 'INT' but all numbers were not whole, I couldn't get to grips with using decimals so it's currently just stored as tinytext

db
0
 
Ray PaseurCommented:
I believe you can select the same column more than once, using the MySQL functions you choose to format the information.  Sorry I do not have time to work on this right now, but the general idea would be something like this:

SELECT
  FORMAT(properties.price,0) as price0
, FORMAT(properties.price,1) as price1
FROM properties...

Also, it would be wise to carry decimal values as data type DECIMAL.  Just a thought... ~Ray
0
 
BrighteyesDesignAuthor Commented:
It does indeed mcnute, just one thing, Dreamweaver is flagging up an error with the first bit of code? When i delete '.' the error marker no longer shows so it's possibly something to do with that? error
Cheers Ray, I see what you're saying there but (there's always a but eh? ) there's not always a range so the first price is not always in one format and the second in another. most of the time there's not even a range...like here...

results
0
 
mcnuteCommented:
Sorry i missed out a comma. So do it like so.

explode('.', $row_jobs['price']);

Open in new window

0
 
Ray PaseurCommented:
...not always a range so the first price is not always in one format and the second in another...
Please post the CREATE TABLE statement along with a brief explanation of the values to be expected in the columns, thanks.  It may be appropriate to change the table definition, but we will not really know until we can see the existing definition and the explanation of what the columns should contain.
0
 
Ray PaseurCommented:
No CREATE TABLE statement, eh?  Ever wonder why you're not getting all the value you could from the dialog here at EE?

You might want to make a Google search for this exact phrase: Should I Normalize My Database.  I know right where you're going (I've seen many novice programmers fall into this black pit) and you're headed into deep trouble.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.