Newbie MySQL/PHP question re output formatting

2005-09-13 Thread Bill Whitacre

I can get this to work just fine:



?php
$number = 23999.39;
print $;
print number_format($number, 2);

?



Comes out $23,999.39

I'd like use the number_format() thingie on an array returned from a  
mysql_query.


My current program snippet looks like:


$res = mysql_query(SELECT org, COUNT(*), SUM(annual_cost) AS  
cost FROM a05

GROUP BY org ORDER BY cost DESC,$dbh);

if (!$res) {
  echo mysql_errno().: . mysql_error ().;
  return 0;
}

print table border=1;

while ($thearray = mysql_fetch_array($res)) {

printf(trtd {$thearray[org]} /td
td align=right {$thearray[COUNT(*)]} /td
td align=right $ {$thearray[cost]} /td/tr);

}

print /table;



and works fine -- see http://ibbmonitor.com/sked_1.php, 3rd block  
of stuff down.


If I replace

{$thearray[cost]}

with

number_format({$thearray[cost]}, 2)

I get

$ number_format(7842554.24, 2)

in the cell where I would expect to get

$ 7,842,554.24

Any idea what I'm doing wrong?

Clearly, I don't understand arrays very well.

Thanks VERY much for any help on this.

bw

---
Bill Whitacre
[EMAIL PROTECTED]


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Newbie MySQL/PHP question re output formatting

2005-09-13 Thread Jigal van Hemert

Bill Whitacre wrote:

printf(trtd {$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:

trtd .number_format($thearray['cost'],2). /tdtd 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

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Newbie MySQL/PHP question re output formatting

2005-09-13 Thread Nuno Pereira

Bill Whitacre wrote:

I can get this to work just fine:



?php
$number = 23999.39;
print $;
print number_format($number, 2);

?



Comes out $23,999.39

I'd like use the number_format() thingie on an array returned from a  
mysql_query.


My current program snippet looks like:


$res = mysql_query(SELECT org, COUNT(*), SUM(annual_cost) AS  
cost FROM a05

GROUP BY org ORDER BY cost DESC,$dbh);

if (!$res) {
  echo mysql_errno().: . mysql_error ().;
  return 0;
}

print table border=1;

while ($thearray = mysql_fetch_array($res)) {

printf(trtd {$thearray[org]} /td
td align=right {$thearray[COUNT(*)]} /td
td align=right $ {$thearray[cost]} /td/tr);

}

print /table;



and works fine -- see http://ibbmonitor.com/sked_1.php, 3rd block  of 
stuff down.


If I replace

{$thearray[cost]}

with

number_format({$thearray[cost]}, 2)

I get

$ number_format(7842554.24, 2)


The issue is that PHP replaces $thearray[cost], with the contents of 
that variable (that is an array, it doesn't matter). But in the second 
case it replaces the same thing ($thearray[cost]), with the contents of 
the variable, but you want to place there the result of the function.

To do date change the first line from

printf(trtd number_format({$thearray[cost]}, 2) /td

to

printf(trtd .number_format({$thearray[cost]}, 2). /td


in the cell where I would expect to get

$ 7,842,554.24

Any idea what I'm doing wrong?

Clearly, I don't understand arrays very well.

Thanks VERY much for any help on this.

bw

---
Bill Whitacre
[EMAIL PROTECTED]




--
Nuno Pereira

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Newbie MySQL/PHP question re output formatting

2005-09-13 Thread Jasper Bryant-Greene

Nuno Pereira wrote:

To do date change the first line from

printf(trtd number_format({$thearray[cost]}, 2) /td

to

printf(trtd .number_format({$thearray[cost]}, 2). /td


I think you mean:

print( trtd . number_format( $thearray['cost'], 2 ) . /td );

As stated by a previous poster to this thread, you shouldn't rely on 
PHP's automagical conversion of nonexistent constants to strings. The 
extra curly braces { } would also likely cause an error in the function 
params, and are extraneous even if they don't.


--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]