Page 1 of 1
Number formatting problem
Posted: Mon May 08, 2006 6:51 pm
by jonah
Using mysql_fetch_row to create some html output. Numbers are integers
which I wish to display with comma separator for thousands.
Code: Select all
for ( $n = 0; $n < $numrows; $n++ ) {
$row2 = mysql_fetch_row($r2);
$land .= '<td align="right">' . implode('</td><td align="right">', $row2) . '</td></tr>';
...
I've tried the following:
Code: Select all
implode('</td><td align="right">', number_format(($row2), ",")
no results show in webpage
Code: Select all
number_format(implode('</td><td align="right">', $row2), ",")
only first column shows in webpage but properly formatted
Code: Select all
$row2 = number_format(mysql_fetch_row($r2), ",");
no results show in webpage
What is the proper way to do this?
Posted: Mon May 08, 2006 8:25 pm
by feyd
number_format() does not process arrays. $row2 is an array. You want the elements of $row2 to be altered by
number_format(), so you may want to use
array_map() or another loop that sets each element to the results from
number_format() for that element.
Posted: Mon May 08, 2006 10:09 pm
by jonah
Is there a way to use print, printf or sprintf to do this?
array_map seems to be overcomplicated.
Posted: Tue May 09, 2006 2:06 pm
by jonah
Took your suggestion and used array_map to call a number_format function
before printing. This displays the integer data just the way I want. However, as
part of the final display, I add two numbers together for each column to get a
total using the formatted data as a base. The totals are now sums of the
truncated versions (with the comma as the demarcation point) of the formatted
numbers.
Code: Select all
function fmt($f) {
return number_format($f,0);
}
for ( $n = 0; $n < $numrows; $n++ ) {
$fld = mysql_num_fields($r2);
$row2 = array_map("fmt", mysql_fetch_row($r2));
$row3 = array_map("fmt", mysql_fetch_row($r3));
$land .= '<td align="right">' . implode('</td><td align="right">', $row2) . '</td></tr>';
echo '<tr><td align="right">' . mysql_result($r1,$n,"PIN") . '</td><td bgcolor="yellow">' . mysql_result($r1,$n,"Name") . '</td></tr>';
echo '<tr><td><br></td><td>Land</td>' . $land;
$bldg .= '<td align="right">' . implode('</td><td align="right">', $row3) . '</td></tr>';
echo '<tr><td><br></td><td>Buildings</td>' . $bldg;
for ( $j=0; $j < $fld; $j++ ) {
$sline .= '<td align="right">------------</td>';
$tot .= '<td align="right">' . ($row2[$j] + $row3[$j]) . '</td>';
$dline .= '<td align="right">========</td>';
}
I realize that I am using formatted data to perform an 'add' and this is the probable cause
of the problem i.e. comma acting as a decimal point. However, I want to slim the code as
much as possible. Can this be done in this or some other fashion without having to create
other variables just to hold the formatted data while using the raw mysql_fetch_row to
perform the math addition?
Posted: Tue May 09, 2006 2:14 pm
by RobertGonzalez
Have you considered reading the mysql query result into an array and using that array for data manipulation? Something along the lines of...
Code: Select all
<?php
while ($row = mysql_fetch_array($result))
{
$myarray[] = $row;
}
for ($i = 0; $i < count($myarray); $i++)
{
$mynum1 = number_format($myarray[$i]['mysql_num_val']); //replace mysql_num_val with your numeric field from the DB
$mynum2 = number_format($myarray[$i]['mysql_num_val_other']); // same here
$mynumsum = $myarray[$i]['mysql_num_val'] + $myarray[$i]['mysql_num_val_other'];
$mynumsum = number_format($mynumsum);
}
?>
This is just an example, but it might put you in a little better position when it comes to format numbers and adding the same, unformatted number.
Posted: Tue May 09, 2006 3:40 pm
by Oren
I can't see that without replying...
Change this:
Code: Select all
for ($i = 0; $i < count($myarray); $i++)
Into this:
Code: Select all
for ($i = 0, $elements = count($myarray); $i < $elements; $i++)
Posted: Tue May 09, 2006 4:28 pm
by RobertGonzalez
Oren wrote:I can't see that without replying...
Change this:
Code: Select all
for ($i = 0; $i < count($myarray); $i++)
Into this:
Code: Select all
for ($i = 0, $elements = count($myarray); $i < $elements; $i++)
What does the second line accomplish that the first does not? It seems like extra overhead to me.
Re: Number formatting problem
Posted: Tue May 09, 2006 5:16 pm
by $phpNut
OK try this
Code: Select all
while ($row2 = mysql_fetch_object($r2)) {
$land .= '<td align="right">' . implode('</td><td align="right">', $row2) . '</td></tr>';
}
That should run through each row for you. and add it to the var $land.
Posted: Tue May 09, 2006 5:18 pm
by jonah
Well, you seem to be using another variable to hold the results of the
addition which I was trying to avoid. While I don't fully understand
the code you supplied, it may be a more elegant solution. I'll experiment
with it, but I think I can modify my code just slightly to achieve the
same result.
Thanks
Posted: Tue May 09, 2006 5:19 pm
by nielsene
Everah wrote:Oren wrote:I can't see that without replying...
Change this:
Code: Select all
for ($i = 0; $i < count($myarray); $i++)
Into this:
Code: Select all
for ($i = 0, $elements = count($myarray); $i < $elements; $i++)
What does the second line accomplish that the first does not? It seems like extra overhead to me.
It "caches" the function call to count so you don't call it once per iteration, only once per loop.
Posted: Tue May 09, 2006 5:25 pm
by Oren
Exactly. It will save time and resources - especially with large loops.
Posted: Tue May 09, 2006 6:03 pm
by RobertGonzalez
OK, thanks for setting me straight.