Math with PHP

They have: 7 posts

Joined: Mar 2004

Hi there

Ok this is what i'm trying to do. I have a db with a field called price, and right know there are 3 differnet values in there 1.45, 2.65, 3.44 ( to give you an idea ) The vaules will always be changing. But i want a script that will add all those up and then add taxes on top of that..

This is what i have.. I'm not getting errors, i'm getting nothing please help me Confused

<?php

   
include (\"dbconnect.php\");
   
$sql = \"SELECT SUM(price) FROM ordertemp;\";
    
   
$result = mysql_query($sql, $db);
   
$row = mysql_fetch_row($result);
   
$subtotal = $row[0];

    echo\"<table border=1>\";
    
   
$GST = $subtotal * 0.07;
   
$PST = $subtotal * 0.08;
   
$total = $subtotal + $GST + $PST;
    
   
$price = sprintf (\"$%.2F\", $price);
   
$subtotal = sprintf (\"$%.2F\", $subtotal);
   
$PST = sprintf (\"$%.2F\", $PST);
   
$GST = sprintf (\"$%.2F\", $GST);
   
$total = sprintf (\"$%.2F\", $total);

      echo\"
        <table  border=1>
              <tr>
            <td>Subtotal</td>
            <td>
$subtotal</td>
            </tr>
            <tr>
               <td>PST(8%)</td>
            <td>
$PST</td>
              </tr>
              <tr>
                <td>GST(7%)</td>
               <td>
$GST</td>
              </tr>
              <tr>
            <td>&nbsp;</td>
               <td><hr></td>
              </tr>
              <tr>
            <td>Total</td>
               <td>
$total</td>
              </tr>
            </table>
  \";

?>

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

After your $sql line:

if (!$sql) { echo mysql_error(); exit; }

Are you getting a result?

Then walk through and echo out your variables and exit so you know where it's breaking down.

Also, you should look into the HEREDOC syntax.

They have: 7 posts

Joined: Mar 2004

I added in the if line right after my sql line and still got nothing.. Uisng phpMyAdmin i tested the sql line and was able to get a total..

But if i run the page i get nothing any more ideas! Confused

They have: 7 posts

Joined: Mar 2004

If i change the code to just this:

<?php

   
include (\"dbconnect.php\");
   
$sql = \"SELECT SUM(price) FROM ordertemp\";;
     if (!
$sql) { echo mysql_error(); exit; }
   
$result = mysql_query($sql, $db);
   
$row = mysql_fetch_row($result);
   
$subtotal = $row[0];   
   echo\"
        <table  border=1>
              <tr>
            <td>Subtotal</td>
            <td>
$subtotal</td>
            </tr>
            </table>
  \";

?>

This works

i'm just not sure why i was unable to add taxes and stuff

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, move it under the next variable and continue on doing this:

i.e.

if (!$result) { echo "nothing for \$result!"; exit; } else { echo $result; exit; }
if (!$row) { echo "nothing for \$row!"; exit; } else { echo $row; exit; }
if (!$subtotal) { echo "nothing for \$subtotal!"; exit; } else { echo $subtotal; exit; }

It's hard to troubleshoot when there is nothing returned at all.

Edit: we're posting at the same time -- use above for each of the $GST variables, and try the HEREDOC syntax.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Oh, geez, you know, I just looked at it and realized it's not your PHP. It's your HTML! You have the table tag opening twice and closing once.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.