PHP calculations help

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I'm fairly versed on php, but I need a little help to get my plan of attack correct.

I want a form with two textareas.

In the first textarea will be invoice numbers separated by commas.

In the second textarea will be the matching dollar amounts separated by commas.

I need the two posted variables to separate out the comma separated values, multiply the dollar amount by 2% and find the difference (no problems here I can do this), and spit out a row of data that has

--invoice number --- amount before discount ---- amount after discount ----- discount dollar

I figured the $_post variable is already in an array and I could use that to cycle through the values and do the calculations. Here's what I was playing with real quick, but I kinda thought I was overthinking it and I got a little messy with displaying the data. Thanks for the help.

caldis.php

Code: [ Select ]

<?php

$inv = $_POST['invnum'];
$amount = $_POST['price'];
$ggg = $_POST['go'];

if ($ggg == 1) {
echo "<table><tr><td>";
$a = split(',', $inv);
foreach ($a as $value) { 
echo($value . "</br> "); // invoice number
}
echo "</td>";
echo "<td>";
$b = split(',', $amount);
foreach ($b as $v) {
$r .= ($v * .02)."</br>"; //discount amount
$paid .= $v - $r ."</br>"; // amount after discount
echo($v . "</br> "); // amount before discount
}
echo "</td>";
echo "<td>";
echo $paid;
echo "</td>";
echo "<td>";
echo $r;
echo "</td>";
echo "</tr></table>";

}


?>
<html>
<body>
<div align="center">
<form method="POST" name="caldis" action="caldis.php">
<table><tr><td>
<textarea name="invnum" cols="50" rows="15" ></textarea>
<textarea name="price" cols="50" rows="15" ></textarea>
<input type="hidden" name="go" size="10" value="1">
<input type="submit" name="submit" value="Calculate">
</td></tr></table>
</form>
</div>
</body>

</html>
  1. <?php
  2. $inv = $_POST['invnum'];
  3. $amount = $_POST['price'];
  4. $ggg = $_POST['go'];
  5. if ($ggg == 1) {
  6. echo "<table><tr><td>";
  7. $a = split(',', $inv);
  8. foreach ($a as $value) { 
  9. echo($value . "</br> "); // invoice number
  10. }
  11. echo "</td>";
  12. echo "<td>";
  13. $b = split(',', $amount);
  14. foreach ($b as $v) {
  15. $r .= ($v * .02)."</br>"; //discount amount
  16. $paid .= $v - $r ."</br>"; // amount after discount
  17. echo($v . "</br> "); // amount before discount
  18. }
  19. echo "</td>";
  20. echo "<td>";
  21. echo $paid;
  22. echo "</td>";
  23. echo "<td>";
  24. echo $r;
  25. echo "</td>";
  26. echo "</tr></table>";
  27. }
  28. ?>
  29. <html>
  30. <body>
  31. <div align="center">
  32. <form method="POST" name="caldis" action="caldis.php">
  33. <table><tr><td>
  34. <textarea name="invnum" cols="50" rows="15" ></textarea>
  35. <textarea name="price" cols="50" rows="15" ></textarea>
  36. <input type="hidden" name="go" size="10" value="1">
  37. <input type="submit" name="submit" value="Calculate">
  38. </td></tr></table>
  39. </form>
  40. </div>
  41. </body>
  42. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

maybe look at this ...

and I like to re-use variables, so instead of this:
Code: [ Select ]
$inv = $_POST['invnum'];
$a = explode(',', $inv);
  1. $inv = $_POST['invnum'];
  2. $a = explode(',', $inv);

I would do the following:
Code: [ Select ]
$a = explode(',',$_POST['invnum']);


but that code looks pretty solid to me ... That's about how I would've done it, but I am not the best PHP coder ever ...
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

Untested, but should work fine...


PHP Code: [ Select ]
<?
 
 
// only execute if POST superglobal is instantiated (ie. on POST request)
if ($_POST) {
 
   // use explode instead of split for performance reasons
   // use preg_split if you need regexs
   
   $invoices = explode(',', $_POST['invoices']);
   $amounts = explode(',', $_POST['amounts']);
   
   foreach($invoices as $i => $invoice) {
     
      // let the foreach loop do half your work by pulling
      // out your $invoice and still incrementing your $i
      // all without using count()
      $amount = $amounts[$i];
     
      // divide by 50 is much cleaner than multiplying by .02
      // round it off to keep your numbers human-safe
      $discount = round($amount / 50, 2);
     
      // calculate your total and format everything proper     
      $total = number_format($amount - $discount);
      $amount = number_format($amount);
      $discount = number_format($discount);
     
      // concatenate output rows...output ONCE instead of hundreds or thousands
      // of times for performance reasons and cleanliness
      // note the newline at the end...it keeps your output code cleaner
      $output .= "<tr><td>$invoice</td><td>$amount</td><td>$total</td><td>$discount</td></tr>\n";
   }
   
   // wrap our output with a table tag
   $output = "<table>$output</table>";
 
}
 
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>Invoices and Dollars</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   </head>
   
   <body>
     
      <?=$output?>
   
      <form action="?" method="post">
         <textarea name="invoices"><?=$_POST['invoices']?></textarea><br />
         <textarea name="amounts"><?=$_POST['amounts']?></textarea><br />
         <input type="submit" value="Calculate" />
      </form>
   </body>
</html>
 
  1. <?
  2.  
  3.  
  4. // only execute if POST superglobal is instantiated (ie. on POST request)
  5. if ($_POST) {
  6.  
  7.    // use explode instead of split for performance reasons
  8.    // use preg_split if you need regexs
  9.    
  10.    $invoices = explode(',', $_POST['invoices']);
  11.    $amounts = explode(',', $_POST['amounts']);
  12.    
  13.    foreach($invoices as $i => $invoice) {
  14.      
  15.       // let the foreach loop do half your work by pulling
  16.       // out your $invoice and still incrementing your $i
  17.       // all without using count()
  18.       $amount = $amounts[$i];
  19.      
  20.       // divide by 50 is much cleaner than multiplying by .02
  21.       // round it off to keep your numbers human-safe
  22.       $discount = round($amount / 50, 2);
  23.      
  24.       // calculate your total and format everything proper     
  25.       $total = number_format($amount - $discount);
  26.       $amount = number_format($amount);
  27.       $discount = number_format($discount);
  28.      
  29.       // concatenate output rows...output ONCE instead of hundreds or thousands
  30.       // of times for performance reasons and cleanliness
  31.       // note the newline at the end...it keeps your output code cleaner
  32.       $output .= "<tr><td>$invoice</td><td>$amount</td><td>$total</td><td>$discount</td></tr>\n";
  33.    }
  34.    
  35.    // wrap our output with a table tag
  36.    $output = "<table>$output</table>";
  37.  
  38. }
  39.  
  40. ?>
  41.  
  42.  
  43. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  44.  
  45. <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
  46.    <head>
  47.       <title>Invoices and Dollars</title>
  48.       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  49.    </head>
  50.    
  51.    <body>
  52.      
  53.       <?=$output?>
  54.    
  55.       <form action="?" method="post">
  56.          <textarea name="invoices"><?=$_POST['invoices']?></textarea><br />
  57.          <textarea name="amounts"><?=$_POST['amounts']?></textarea><br />
  58.          <input type="submit" value="Calculate" />
  59.       </form>
  60.    </body>
  61. </html>
  62.  
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Sorry for the delayed response. I was out of town for the holiday. Hope everyone had a good thanksgiving.

Truce,

That's great work. Exactly what I needed.


Thanks so much for your help guys.

-we can close this post.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 68 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.