Number formatting problem

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
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Number formatting problem

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Post by jonah »

Is there a way to use print, printf or sprintf to do this?

array_map seems to be overcomplicated.
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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++)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Re: Number formatting problem

Post 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.
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Exactly. It will save time and resources - especially with large loops.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

OK, thanks for setting me straight.
Post Reply