Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

PHP Table Data Total

Hi,

I have one totals.php file that displays Totals for some columns. I have another report.php file that should display the totals from the totals.php file. Right now, the report.php file will only get the first result value, but not the total amount of results for the columns.

This is the code for totals.php:

<?php

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
      echo ('<td>'.$val[1].'</td>');
      echo ('<td>'.$val[2].'</td>');
      echo ('<td>'.$val[3].'</td>');
    echo ('</tr>');
}

$empty1 = 0;  $sum3 = 0; $avg4 = 0;  $empty2 = 0; $empty3 = 0;

foreach ($resultArray as $val) {
  if(empty($val[1])) $empty1++;
  if(empty($val[2])) $empty2++;
  if(empty($val[3])) $empty3++;
  $sum++;
  $sum3 += $val[2];
  $avg4 += $val[3];
}
echo "<tr>
<td>".($sum-$empty1)."</td>
<td>$sum3</td> // This total needs to be displayed in report.php
<td>".($avg4/$sum)."%</td> // This total needs to be displayed in report.php
</tr>";


} // end if results
echo ('</table>');

?>


Below is the other code for the report.php file where I want to display the Total from the totals.php code above:


<?php

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

echo "<tr>";
echo "<td class=\"th\" >col1</td>";
echo "<td class=\"th\" >col2</td>";
echo "<td class=\"th\" >col3</td>";

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>'); //total
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>'); //total for $sum3 goes here
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'</td>'); //total for .($avg4/$sum). goes here
    echo ('</tr>');
}


} // end if results
echo ('</table>');

?>

0
fcruz5
Asked:
fcruz5
  • 4
  • 3
1 Solution
 
nizsmoDeveloperCommented:
I don't see how you are passing the calculated totals:
$sum3</td> // This total needs to be displayed in report.php
<td>".($avg4/$sum)."%</td> // This total needs to be displayed in report.php

into report.php? If you don't pass it along, how can the values be displayed?

You have several options to pass the values along, however the easiest one is to setting it a session variable so that the variable is accessible through all your scripts:

// in totals.php
session_start(); // this is needed on every page at the start, has to be first line
// code here...
echo "<tr>
<td>".($sum-$empty1)."</td>
<td>$sum3</td> // This total needs to be displayed in report.php
<td>".($avg4/$sum)."%</td> // This total needs to be displayed in report.php
</tr>";


$_SESSION["sum3"] = $sum3;
$_SESSION["avgdsum"] = $avg4/$sum;

// more code here

session_close();


// in report.php
session_start();
// your code here...

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>'); //total
      echo ('<td class=' . $css_class . ' align=default >'.$_SESSION["sum3"].'</td>'); //total for $sum3 goes here
      echo ('<td class=' . $css_class . ' align=default >'.$_SESSION["avgdsum"].'</td>'); //total for .($avg4/$sum). goes here
    echo ('</tr>');
}

// More code here...
session_close();


Hope this willl work for you.
0
 
fcruz5Author Commented:
Hi, thanks for the reply.  I added the sessions but I keep getting this error:

Fatal error: Call to undefined function: session_close()

It also returns the same result over and over again.
0
 
nizsmoDeveloperCommented:
Sorry! It should be: session_write_close().

What do you mean it displays the same result over and over again? Since you did not provide the queries for your database, i am not sure what you are trying to do. Why don't you just calculate the average and the sum in the report.php?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
fcruz5Author Commented:
Because the average and sum are being pulled from totals.php. Its just needs to display them.

session_write_close(); fixed the error. I will overlook at my queries again since it repeats the same result over and over again.
0
 
fcruz5Author Commented:
It displays the correct total in the first row. But that total repeats itself for the rest of the results.
0
 
nizsmoDeveloperCommented:
Did you end up solving the problem? The reason it repeats itself is because the total is only passed in once, so there is only 1 value for total.

In order to display different totals, assign an array and put all calculated totals in there and pass it through the session variable.

Hope this help!
0
 
fcruz5Author Commented:
Hi Nizsmo,

Thanks for you help on this. I ended solving the problem. Now I am just working on getting it to display different totals. I am trying to do what you suggested. Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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