Still new to php..number formatting query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Still new to php..number formatting query

Post by Flashart »

Hello all

Ok so I have read the manual for number formatting but i can't think of how to implement this into my code.

Basically what i am doing is retrieving data from a mysql database which then displays my currency data as 0.331904

What I have been trying to do is getting it to display £0.33.

The results populate a table that has this code

Code: Select all

//Table to hold the data
    echo "<table id='results_table' class='tablesorter'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th>Account</th>";
    echo "<th>Campaign</th>";
    echo "<th>Adgroup</th>";
    echo "<th>Impressions</th>";
    echo "<th>Clicks</th>";
    echo "<th>CTR</th>";
    echo "<th>CPC</th>";
    echo "<th>Cost</th>";
    echo "<th>Avg_Position</th>";
    echo "<th>Conversions</th>";    
    echo "<th>Conversions_mpc</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";
 
 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
 
 
{
    
    echo "<tr onmouseover=\"mouse_event(this, 'hlt');\" onmouseout=\"mouse_event(this, '');\">";
    //echo "<td>{$row['date']}</td>";
    echo "<td>{$row['account']}</td>";
    echo "<td>{$row['campaign']}</td>";
    echo "<td>{$row['adgroup']}</td>";
    echo "<td>{$row['impressions']}</td>";
    echo "<td>{$row['clicks']}</td>";
    echo "<td>{$row['ctr']}%</td>";
    echo "<td>{$row['cpc']}</td>";
    echo "<td>{$row['cost']}</td>";
    echo "<td>{$row['avg_position']}</td>";
    echo "<td>{$row['conversions']}</td>";
    echo "<td>{$row['conversions_mpc']}</td>";
    
    echo "</tr>";
    
    
    
    
}
 
?>
I can't think of where to put the number_format code. I have tried various places but it doesn't work.
I'm sure I am missing something simple. Can someone point me in the right direction?

Many thanks and kind regards
Peter
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Still new to php..number formatting query

Post by manohoo »

Code: Select all

 
$number = 123.456;
echo "£".round($number,2);
 
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Still new to php..number formatting query

Post by Charles256 »

Check out the examples at http://us3.php.net/manual/en/function.money-format.php and for locale read http://us3.php.net/manual/en/function.setlocale.php . Pay particular attention to the notes section to make sure you find your locale. Of course you could just use http://us3.php.net/number_format and output the symbol of your choice before calling the function. Let me know if that answers your question after reading it. If not, post your new attempts code and I'll help you tweak it.

The post above me should also work. :)
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Still new to php..number formatting query

Post by Flashart »

Hi thanks

I have read that particular page a lot! Doesn't really help though because I can't visualise how to implement that into where I have for example

Code: Select all

echo "<td>{$row['cost']}</td>";
.

When i do a simple example such as what they show, that works fine (where you explicitly declare the variable amount at the beginning) but my variable (£cost for example) isn't explicitly declared (ie: $cost = 123.56;) It's retrieved dynamically via mysql from the query I have:

Code: Select all

$query = "SELECT date,account,campaign,adgroup, sum(impressions)impressions, sum(clicks)clicks,sum(clicks/impressions)ctr,sum(cost)cost,sum(cost/clicks)cpc,avg_position,sum(conversions)conversions,sum(conversions_mpc)conversions_mpc FROM adgroup_data WHERE adgroup LIKE '".$adgroup."' AND date>= '".$from_date."' AND date <='".$to_date."' group by adgroup";
 
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
 
 

so I know the cost variable is in the $result. However I'm not sure how to retrieve that specifically from the $result to implement the number_format. I did think of going down the multi-array paths but that is just too much for my current knowledge!

I hope I have explained my confusion a little better? Any help would be great!

Thanks
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Still new to php..number formatting query

Post by Charles256 »

Basically you'd do something like this (I'm assuming your queries work :) )

Code: Select all

 
$information=mysql_fetch_object($resource);
$cost=$information->cost; // -> cost since you stored th sum as the field "Cost" this should grab it.  might want to type cast cost by doing (float)$information->cost   but that's because I don't trust the database.
// now treat cost as if you had declared it $cost=154.56 since that's basically what it is. The fact that it can change since it's a mysql result doesn't make it magical. :)
 
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Still new to php..number formatting query

Post by manohoo »

Code: Select all

echo "<td>£".{round($row['cost'],2)}."</td>";
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Still new to php..number formatting query

Post by manohoo »

or... if you want to do it in MySQL:

Code: Select all

SELECT CONCAT('£',numeric_field) FROM `table`
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Still new to php..number formatting query

Post by Flashart »

Thanks for your help.

However I get an error message using this code
manohoo wrote:

Code: Select all

echo "<td>£".{round($row['cost'],2)}."</td>";
PHP tells me ther eis an "unexpected { in line 174" I have copied and pasted this example and tried various iterations with no success. I would be grateful for any help.

Many thanks.

Peter
Post Reply