List:General Discussion« Previous MessageNext Message »
From:Jigal van Hemert Date:September 13 2005 3:01pm
Subject:Re: Newbie MySQL/PHP question re output formatting
View as plain text  
Bill Whitacre wrote:
>>         printf("<tr><td> {$thearray[org]} </td>
>>         <td align=right> {$thearray["COUNT(*)"]} </td>
>>         <td align=right> $ {$thearray[cost]} </td></tr>");
> If I replace
> {$thearray[cost]}
> with
> number_format({$thearray[cost]}, 2)

Although this is a MySQL mailing list and your problem is not MySQL 
related, but a PHP question I'll give you a brief answer.

PHP does not evaluate functions inside a double quoted string, so you 
should use:
"<tr><td> ".number_format($thearray['cost'],2)." </td><td
align......."

Furthermore, using $thearray[cost] is not advisable; PHP will try to 
find a constant named 'cost' and if it can't find one it will use the 
string itself as the value. Use a real string (quoted) instead of 
relying on this feature:
$thearray['cost'] or $thearray["cost"]

Also you can use aliases in your query to avoid things like 
$thearray["COUNT(*)"];
Use something like this in your query:
SELECT .... COUNT(*) AS `count` .... FROM ....
and you can use $thearray['count'] instead

As a last pointer, printf() is pretty much useless here since you don't 
use any variable formatting features of printf() here. print() or echo() 
are more suitable in this case.

Regards, Jigal
Thread
Newbie MySQL/PHP question re output formattingBill Whitacre13 Sep
  • Re: Newbie MySQL/PHP question re output formattingJigal van Hemert13 Sep
  • Re: Newbie MySQL/PHP question re output formattingNuno Pereira13 Sep
    • Re: Newbie MySQL/PHP question re output formattingJasper Bryant-Greene13 Sep