yamya
asked on
PHP number output from mysql query calculation
How do I write the code for PHP number formatting when the value is the result of a mysql COUNT,SUM, MAX or AVG query?
I'd like thousands separators and two decimal places.
Here is the code I use right now that generates just a numeric string with no separators or decimal places.
I'm stuck on how to turn the calculated result into a formatted $var before calling it to print $myRow[ ].
Thanks!
Amy
I'd like thousands separators and two decimal places.
Here is the code I use right now that generates just a numeric string with no separators or decimal places.
I'm stuck on how to turn the calculated result into a formatted $var before calling it to print $myRow[ ].
Thanks!
Amy
echo "<div class='boxit'>";//start div wrapper for the next report
//query a list of sales reps with highest expected value
$query = "SELECT RSM, MAX(ExpectedValue) FROM dbtable WHERE RSM NOT LIKE 'Projects' GROUP by RSM";
$result=mysql_query($query) or die(mysql_error());
if ($myrow = mysql_fetch_array($result)) {
echo "<table border='1'>";
echo "<span class='bold'>Highest Value <br />by RSM </span><br /><a href='#top'>Back to top</a><br />";
echo "<tr><td class=''>RSM</td><td>ExpVal</td></tr>\n";
do {
printf("<tr><td class=''>%s</td><td class='cellRight'>%s</td></tr>\n", $myrow["RSM"], $myrow['MAX(ExpectedValue)']);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
echo "</div>"; //Close the div report wrapper
} else {
echo "$ref: That record appears to be unavailable";
}
Use money_format
<?php
$number = 1234.56;
setlocale(LC_MONETARY, "en_US");
echo money_format("The price is %i", $number);
?>
Or
$english_format_number = number_format($number);
// 1,235
<?php
$number = 1234.56;
setlocale(LC_MONETARY, "en_US");
echo money_format("The price is %i", $number);
?>
Or
$english_format_number = number_format($number);
// 1,235
ASKER
Please help me understand WHERE is this code to be inserted?
I tried setting it before the printf statement - as
number_format($ExpectedVal ue)
no good
I tried it these two ways, no good:
$myrow[number_format("Expe ctedValue" )]
$myrow["number_format(Expe ctedValue) "]
the other query result is a calculated field which I think needs different treatment?
$myrow['SUM(ExpectedValue)
thanks!
I tried setting it before the printf statement - as
number_format($ExpectedVal
no good
I tried it these two ways, no good:
$myrow[number_format("Expe
$myrow["number_format(Expe
the other query result is a calculated field which I think needs different treatment?
$myrow['SUM(ExpectedValue)
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
John
EVerything i read says that I cannot format a number that is the result of a calculation.
SUM(expectedValue) is a calculation.
so i think the trick must be to convert the result AFTER THe query but BEFORE The printf statement.
this did not work -
$evf=("SUM(ExpectedValue") ;
number_format($myrow("$evf ");
All input welcome!
Thanks
EVerything i read says that I cannot format a number that is the result of a calculation.
SUM(expectedValue) is a calculation.
so i think the trick must be to convert the result AFTER THe query but BEFORE The printf statement.
this did not work -
$evf=("SUM(ExpectedValue")
number_format($myrow("$evf
All input welcome!
Thanks
ASKER
Perfect! (after finding my typo).
THank you!@!
THank you!@!
Please check the php number_format() function. This should do exactly what you require
Hope this helps,
John