• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

PHP Table Row total not displaying correctly

Hi,

I have the following table with its results and totals for each column on last the row. All the totals display correctly except for the third one. The third column total displays as "0". I don't know if the "$" dollar and "-" minus signs have anything to do with it. How can I make it display the total for the third column correctly?


1          20          $1,594
9          55          $-
0          18          $2,594
2          11          $92
8          96          $-

20        200        0 <-------- this is the total for the third column which is not displaying correctly.

Here is the PHP code:

<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td>Col-1</td>";
echo "<td>Col-2</td>";
echo "<td>Col-3</td>";
echo "</tr>";

    echo ('</tr>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'&nbsp</td>');
    echo ('</tr>');
}

//totals

foreach ($resultArray as $val) {
  $sum++;
  $sum1 += $val[1];
  $sum2 += $val[2];
  $sum3 += $val[3];
}


echo "<tr>
<td></td>
<td>$sum1</td>
<td>$sum2</td>
<td>$sum3</td>
</tr>";

echo "</table>";
      
?>
0
fcruz5
Asked:
fcruz5
  • 6
  • 3
1 Solution
 
Rok-KraljCommented:
Sure $ have to do!!! You cannot calculate an expression with $ sign.
0
 
Rok-KraljCommented:
Here is your php code:


<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td>Col-1</td>";
echo "<td>Col-2</td>";
echo "<td>Col-3</td>";
echo "</tr>";

    echo ('</tr>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'&nbsp</td>');
    echo ('</tr>');
}

//totals

foreach ($resultArray as $val) {
  $sum++;
  $sum1 += $val[1];
  $sum2 += $val[2];

$spec=substr($val[3], 1);

if (strlen($spec)==1 and $spec[0]=='-') {$spec.='-';}

  $sum3 += $spec;
}


echo "<tr>
<td></td>
<td>$sum1</td>
<td>$sum2</td>
<td>$sum3</td>
</tr>";

echo "</table>";
     
?>
0
 
Rok-KraljCommented:
Pardon, wong... Correct code:

<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td>Col-1</td>";
echo "<td>Col-2</td>";
echo "<td>Col-3</td>";
echo "</tr>";

    echo ('</tr>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'&nbsp</td>');
    echo ('</tr>');
}

//totals

foreach ($resultArray as $val) {
  $sum++;
  $sum1 += $val[1];
  $sum2 += $val[2];

$spec=substr($val[3], 1);

  $sum3 += $spec;
}


echo "<tr>
<td></td>
<td>$sum1</td>
<td>$sum2</td>
<td>$sum3</td>
</tr>";

echo "</table>";
     
?>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kiran Paul VJComputer EngineerCommented:
or try this

<?php
$replace = array("$",",");

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td>Col-1</td>";
echo "<td>Col-2</td>";
echo "<td>Col-3</td>";
echo "</tr>";

    echo ('</tr>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'&nbsp</td>');
    echo ('</tr>');
}

//totals

foreach ($resultArray as $val) {
  $sum++;
  $sum1 += $val[1];
  $sum2 += $val[2];
  //$sum3 += $val[3];
  $sum3 += str_replace($replace,"",$val[3]);
}


echo "<tr>
<td></td>
<td>$sum1</td>
<td>$sum2</td>
<td>$sum3</td>
</tr>";

echo "</table>";
     
?>
0
 
Cornelia YoderArtistCommented:
This is not a numeric value:     $-

Therefore php will treat it as a string and not do any arithmetic, so the corresponding $sum value will be a string as well.
0
 
Rok-KraljCommented:
kiravj, your code is wrong. I corrected it for you:

<?php
$replace = array("$",",");

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td>Col-1</td>";
echo "<td>Col-2</td>";
echo "<td>Col-3</td>";
echo "</tr>";

    echo ('</tr>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'&nbsp</td>');
    echo ('</tr>');
}

//totals

foreach ($resultArray as $val) {
  $sum++;
  $sum1 += $val[1];
  $sum2 += $val[2];
  //$sum3 += $val[3];
  $sum3 += str_replace('$',"",$val[3]);
}


echo "<tr>
<td></td>
<td>$sum1</td>
<td>$sum2</td>
<td>$sum3</td>
</tr>";

echo "</table>";
     
?>
0
 
Rok-KraljCommented:
Uh, i see... I missed your upper line...

Why are you escaping comma too?
0
 
Kiran Paul VJComputer EngineerCommented:
yes because $ and , can cause errors when suming ;)
0
 
Rok-KraljCommented:
Oh, i see... He is using comma for separating thousands....
0
 
Kiran Paul VJComputer EngineerCommented:
yes, so i removed , also.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now