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
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";
}

Open in new window

yamyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

john-formbyCommented:
Hi,

Please check the php number_format() function.  This should do exactly what you require

Hope this helps,

John
Om PrakashCommented:
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
yamyaAuthor Commented:
Please help me understand WHERE is this code to be inserted?

I tried setting it before the printf statement - as
number_format($ExpectedValue)
no good

I tried it these two ways, no good:

$myrow[number_format("ExpectedValue")]

$myrow["number_format(ExpectedValue)"]


the other query result is a calculated field which I think needs different treatment?
$myrow['SUM(ExpectedValue)


thanks!
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

john-formbyCommented:
Hi,

Would be something like:

number_format($myrow[SUM(ExpectedValue)])

Hope this helps,

John

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yamyaAuthor Commented:
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
yamyaAuthor Commented:
Perfect! (after finding my typo).

THank you!@!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.