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?
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
0
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
0
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!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

john-formbyCommented:
Hi,

Would be something like:

number_format($myrow[SUM(ExpectedValue)])

Hope this helps,

John
0

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
0
yamyaAuthor Commented:
Perfect! (after finding my typo).

THank you!@!
0
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.

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.