?
Solved

Displaying Difference for Totals

Posted on 2007-10-01
6
Medium Priority
?
243 Views
Last Modified: 2013-12-13
I have for example the table data results below (the first 3 columns):

Division          Quota           Total          New Column
1                    1600             1610          10
2                    400               220            -180
3                    900               800            -100
4                    850               900            50
5                    1300             1200          -100

I want to add a fourth column (New Column) that will calculate the difference between the "Quota" column and the "Total" column. For example: the first row has 1600 for the Quota and 1610 for the Total. So it should display the difference which is 10.

The second row for example: has 400 for the Quota and 220 for the Total. So it falls into the negative and displays -180.

So basically, I need it to display the amount if it was over or under. How would something like this be done?

Here is the PHP code:

<?php

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

echo "<tr>";
echo "<td class=\"th\" >Division</td>";
echo "<td class=\"th\" >Quota</td>";
echo "<td class=\"th\" >Total</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>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'</td>');
    echo ('</tr>');
}


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

?>
0
Comment
Question by:fcruz5
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:frin
ID: 19991872
Add this, what it does is return absolute value of subtraction of the two desired columns (-1 becomes 1):
      echo ('<td class=' . $css_class . ' align=default >'.abs($val[3]-$val[2]).'</td>');
0
 

Author Comment

by:fcruz5
ID: 19992615
Thanks, it shows the difference now. But, theres one more thing that I am trying to display.

Division          Quota           Total          New Column
1                    1600             1610          10
2                    400               220            -180
3                    900               800            -100
4                    850               900            50
5                    1300             1200          -100

For example, take a look at the 2nd row with 400 as the Quota and 220 for the Total. For the totals that are below the Quota like this one, it should display a (-) symbol in front of the number. The difference is 180. Can it display as -180 like the example above?

0
 
LVL 5

Expert Comment

by:frin
ID: 19992704
I am not sure I quite follow you. First you need absolute value, now not ;) Unless you want a fifth column.

In that case, it's just a matter of subtracting the right value from the other:

      echo ('<td class=' . $css_class . ' align=default >'.($val[3]-$val[2]).'</td>');

Two examples:

$val[3] = 220
$val[2] = 400
$val[3]-$val[2] = 220-400 = -180

$val[3] = 1610
$val[2] = 1600
$val[3]-$val[2] = 1610-1600 = 10

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.

 

Author Comment

by:fcruz5
ID: 19992891
The first example below that you gave me displays the correct differences like this:

echo ('<td class=' . $css_class . ' align=default >'.abs($val[3]-$val[2]).'</td>');

Division          Quota           Total          New Column
1                    1600             1610          10
2                    400               220            180
3                    900               800            100
4                    850               900            50
5                    1300             1200          100

=========================================================

The only thing that is missing is the (-) sign in front of the values for the Totals that fall under the Quota. That way you can see if the Division was over or under the Quota.

Any number under the "Total" column that is below the number under the Quota column will have minus (-) sign in front of it like in rows 2, 3, and 5. See the below example.

The total 220 falls under the 400 Quota so the difference 180 displays as -180
The total 800 falls under the 900 Quota so the difference 100 displays as -100
The total 1200 falls under the 1300 Quota so the difference 100 displays as -100

Division          Quota           Total          New Column
1                    1600             1610          10
2                    400               220            -180
3                    900               800            -100
4                    850               900            50
5                    1300             1200          -100

0
 
LVL 5

Accepted Solution

by:
frin earned 2000 total points
ID: 19993459
Yes, you need to drop abs :) Abs removes the minuses. So if you want to keep those minuses, the code should look like this:

<?php

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

echo "<tr>";
echo "<td class=\"th\" >Division</td>";
echo "<td class=\"th\" >Quota</td>";
echo "<td class=\"th\" >Total</td>";
echo "<td class=\"th\" >New Column</td>";
echo "</tr>";

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>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[3].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.($val[3]-$val[2]).'</td>');
    echo ('</tr>');
}


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

?>
0
 

Author Comment

by:fcruz5
ID: 19993849
Thanks
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question