Results 1 to 8 of 8

Thread: PHP Calculator Help

  1. #1
    Join Date
    Sep 2006
    Posts
    154
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default PHP Calculator Help

    First post in the forum...scary.
    I'm having trouble with my PHP calculator. Heres the code:
    PHP Code:
    <body>
    <?php
    $eqString 
    $_POST["equation"];                  //Postdata stored into $eqString.
    $eqString str_replace("+"" + "$eqString);  //Add spaces before and after +.
    $eqString str_replace("-"" - "$eqString);  //Add spaces before and after -.
    $eqString str_replace("*"" * "$eqString);  //Add spaces before and after *.
    $eqString str_replace("/"" / "$eqString);  //Add spaces before and after /.
    $pieces explode(" "$eqString);               //Put each term or operator of $eqString and put int into an array in $pieces.
    $i1 0;                                         //Set first counting variable to 0.
    $i2 0;                                         //Set second counting variable to 0.
    foreach ($pieces as $array) {                    //This works for each variable in the array $pieces.
      
    if ($array == "+") {                           //Checks to see if the current variable is +.
        
    $operator[$il] = $array;                        //If so, then it sets the current operator variable as +.
    //    echo $operator[$i1];  //Test
        
    $i1++;                                       //Adds one to the first counting number.
      
    } elseif ($array == "-") {                     //Checks to see if the current variable is -.
        
    $operator[$il] = $array;                        //If so, then it sets the current operator variable as -.
    //    echo $operator[$i1];  //Test
        
    $i1++;                                       //Adds one to the first counting number.
      
    } elseif ($array == "*") {                     //Checks to see if the current variable is -.
        
    $operator[$il] = $array;                        //If so, then it sets the current operator variable as *.
    //    echo $operator[$i1];  //Test
        
    $i1++;                                       //Adds one to the first counting number.
      
    } elseif ($array == "/") {                     //Checks to see if the current variable is -.
        
    $operator[$il] = $array;                        //If so, then it sets the current operator variable as /.
    //    echo $operator[$i1];  //Test
        
    $i1++;                                       //Adds one to the first counting number.
      
    } elseif ($array >= 0) {                       //Checks to see if the current variable is a number.
        
    $term[$i2] = $array;                         //If so, then it sets the current term variable as that number.
    //    echo $term[$i2];  //Test
        
    $i2++;                                       //Adds one to the second counting number.
      
    } else {
        
    $answer NULL;                              //Otherwise, it returns NULL.
      
    }
    }
    print_r($term);  //Test
    print_r($operator);  //Test
    //function smath(&$term, &$operator, &$answer) {
        
    echo $operator[0];  //Test
      
    if ($operator[0] == "+") {
        
    $answer $term[0] + $term[1];
        echo 
    $answer;  //Test
      
    } elseif ($operator[0] == "-") {
        
    $answer $term[0] - $term[1];
        echo 
    $answer;  //Test
      
    } elseif ($operator[0] == "*") {
        
    $answer $term[0] * $term[1];
        echo 
    $answer;  //Test
      
    } elseif ($operator[0] == "/") {
        
    $answer $term[0] / $term[1];
        echo 
    $answer;  //Test
      
    } else {
        
    $answer NULL;
      }
      
    $i 0;
      foreach (
    $term as $array) {
        if (
    $i 0) {
          if (
    $operator[$i] == "+") {
            
    $answer $answer $array;
          } elseif (
    $operator[$i] == "-") {
            
    $answer $answer $array;
          } elseif (
    $operator[$i] == "*") {
            
    $answer $answer $array;
          } elseif (
    $operator[$i] == "/") {
            
    $answer $answer $array;
          } else {
            
    $answer NULL;
          }
        }
        
    $i++;
      }
    //}
    ?>
    <form name="calculator" action="calcFrame.php" target="_self" method="POST">
    <fieldset>
    <legend>Calculator: (Currently Broken)</legend>
    <input type="text" name="equation" maxlength="31" value="<?php echo $answer?>" />
    <input type="submit" value="Enter" />
    <p>Note:  This calculator does not follow order of operations.</p>
    </fieldset>
    </form>
    </body>
    I think for some reason, it doesn't save the operators or terms to $operator[0] or $term[0]. But I have no clue why. Any thoughts?

  2. #2
    Join Date
    Feb 2006
    Location
    Myrtle Beach, SC USA!
    Posts
    841
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    Ill review your code and see what happens when i have time tonight, but the first thing i would do is is print all your varables and arrays after you add-remove from them like

    PHP Code:
    print $var;
    print_r($array); 
    print_r is my favorite tool for troubleshooting as it dumpsthe entire array wiht all keys and values. But if that leads you no where ill look at it when my son goes to sleep.

  3. #3
    Join Date
    Sep 2006
    Posts
    154
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Alright. Thanks. I'll try that now.

    K, the problem seems to be recording the operator value as an array.

  4. #4
    Join Date
    Nov 2006
    Location
    California, USA
    Posts
    336
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I fixed

    PHP Code:
     <body>
    <?php
    $action   
    $_POST['action'];
    if(
    $action == 1) {
        
    $eqString $_POST['equation'];                  //Postdata stored into $eqString.
        
    $eqString ereg_replace(" """$eqString); //Makes it so that if they put in " + " the code below won't change it into "  +  "
        
    $eqString ereg_replace("\+"" + "$eqString);  //Add spaces before and after +.
        
    $eqString ereg_replace("-"" - "$eqString);  //Add spaces before and after -.
        
    $eqString ereg_replace("\*"" * "$eqString);  //Add spaces before and after *.
        
    $eqString ereg_replace("/"" / "$eqString);  //Add spaces before and after /.
        
    $pieces explode(" "$eqString);               //Put each term or operator of $eqString and put int into an array in $pieces.
        
    $i1 0;                                         //Set first counting variable to 0.
        
    $i2 0;                                         //Set second counting variable to 0.
        
    foreach ($pieces as $array) {                   //This works for each variable in the array $pieces.
            
    if (($array == "+") || ($array == "-") || ($array == "*") || ($array == "/")) {                           //Checks to see if the current variable is +.                     //If so, then it sets the current operator variable as +.
                
    $operator[$i1] = $array;
                
    //echo $operator[$i1]."<br>";  //Test
                
    $i1++;                                       //Adds one to the first counting number.
            
    } elseif ($array >= 0) {                       //Checks to see if the current variable is a number.
                
    $term[$i2] = $array;                         //If so, then it sets the current term variable as that number.
                //echo $term[$i2]."<br>";  //Test
                
    $i2++;                                       //Adds one to the second counting number.
            
    } else {
                
    $answer NULL;                              //Otherwise, it returns NULL.
            
    }
        }
        
    //print_r($term);  //Test
        //print_r($operator);  //Test
        //function smath(&$term, &$operator, &$answer) {
        //echo $operator[0];  //Test
        
    $key1 0;
        
    $key2 1;
        
    $answer $term[0];
        while(
    array_key_exists($key1$operator)) {
            
    //echo "operator[".$key1."]: ".$operator[$key1]."<br>";
            //echo "answer: ".$answer."<br>";
            
    if(array_key_exists($key2$term)) {
                
    //echo "term[".$key2."]: ".$term[$key2]."<br>";
                
    if ($operator[$key1] == "+") {
                    
    $answer $answer $term[$key2];
                    
    //echo $answer;  //Test
                
    } elseif ($operator[$key1] == "-") {
                    
    $answer $answer $term[$key2];
                    
    //echo $answer;  //Test
                
    } elseif ($operator[$key1] == "*") {
                    
    $answer $answer $term[$key2];
                    
    //echo $answer;  //Test
                
    } elseif ($operator[$key1] == "/") {
                    
    $answer $answer $term[$key2];
                    
    //echo $answer;  //Test
                
    } else {
                    
    $answer NULL;
                }
            }
            
    $key1++;
            
    $key2++;
        }
        
    //}
    }
    echo 
    "<br>The Answer For "$eqString." is ".$answer.".<br><hr>";
    ?>
    <form name="calculator" action="calcFrame.php" target="_self" method="POST">
    <fieldset>
    <legend>Calculator: (Fixed By TOB)</legend>
    <input type="text" name="equation" maxlength="31"/>
    <input type="hidden" name="action" value="1">
    <input type="submit" value="Enter" />
    <p>Note:  This calculator does not follow order of operations.</p>
    </fieldset>
    </form>
    </body>

  5. #5
    Join Date
    Sep 2006
    Posts
    154
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    So what was the matter? And yea I'll include credits to you.

  6. #6
    Join Date
    Nov 2006
    Location
    California, USA
    Posts
    336
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Not entirely sure sorry :P. I think it was the way you set the values, I had made a couple change before that version so I can't remember what it was you did and what I did.

  7. #7
    Join Date
    Sep 2006
    Posts
    154
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Lol all right.

  8. #8
    Join Date
    Jan 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Thumbs up

    you may check php calculator script to find the script you need!
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Compound Interest Calculator</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <?php
        
    if (isset($_POST['balance'])) {
            
    $orgbal $_POST['balance'];
            
    $orgint =  $_POST['interest'];
         } else {
             
    // starting values for the calculator, change thi sto suit your audience
            
    $orgbal '3000';
            
    $orgint =  '19.99';
         }
    ?>

      

    <div><form id="calculate" name="calculate" method="post" action="<?php print_r($_SERVER['SCRIPT_NAME']); ?>">
    <table id="calculate" width="600px" border="0" cellpadding="0" cellspacing="0"  class="calculatortable">
                  <tr>
                    <th colspan="2">
                      <div align="left">Compound Interest Calculator                 
                    </div></th>
                  </tr>
                  <tr>
                    <td colspan="2">
                          Enter your balance  and interest rate to calculate the compound interest you will be charged on a         
                    daily, weekly, monthly and yearly total.</td>
                  </tr>
                  <tr>
                    <td width="50%"><strong>Balance</strong></td>
                    <td width="50%">$
                    <input id="user_interest" name="balance" type="text" value="<?php echo $orgbal ?>" size="7" maxlength="7" /></td>
                  </tr>
                  <tr>
                    <td width="50%"><strong>Rate</strong></td>
                    <td width="50%">&nbsp;&nbsp;&nbsp;
                      <input id="user_total" name="interest" type="text" value="<?php echo $orgint ?>" size="5" maxlength="5"   />%
                    </td>
                  </tr>
                  <tr>
                    <td width="50%">&nbsp;</td>
                    <td><input type="submit" name="Submit" value="Calculate Interest" style="float:right;" /></td>
                  </tr>
                  <tr>
                    <td colspan="2">
           <span class="smltxt"> This calculation does not include credit card fees, other charges and  repayments to show only interest charges. Created by <a href="http://www.bestratecreditcards.com.au">Best Rate Credit Cards Australia</a></span>
                    </td>
                  </tr>
              </table>

              </form>

    </div>                    
                            

    <br />


    <?php
    if (isset($_POST['balance'])) {


    $bal $_POST['balance'];
    $int $_POST['interest']/100;
    $per 12;

    $interestcharged $bal*(pow(1+($int/$per),$per)-1);

    $dailyi $interestcharged/365;
    $weeklyi $interestcharged/52;
    $monthlyi $interestcharged/12;
    ?>

    <div>
            <table width="600px" border="0" cellspacing="0" cellpadding="0" class="calculatortable">
                  <tr> 
                    <th colspan="3"><div align="left">Calculated Interest</div></th>
                  </tr>
                  <tr>
                    <td width="20%"><strong>Interest Charged</strong></td>
                    <td width="20%"><strong>Amount</strong></td>
                    <td width="20%"><strong>New Balance </strong></td>
                  </tr>
                  <tr>
                    <td width="20%">Daily</td>
                    <td width="20%"><span class="mtxtbold">$<?php echo number_format($dailyi2?></span></td>
                    <td width="20%" class="mtxtbold"><strong>$<?php echo number_format($dailyi $bal2?></strong></td>
                  </tr>
                  <tr>
                    <td width="20%">Weekly</td>
                    <td width="20%"><span class="mtxtbold">$<?php echo number_format($weeklyi2?></span></td>
                    <td width="20%" class="mtxtbold"><strong>$<?php echo number_format($weeklyi $bal2?></strong></td>
                  </tr>
                  <tr>
                    <td width="20%">Monthly</td>
                    <td width="20%"><span class="mtxtbold">$<?php echo number_format($monthlyi2?></span></td>
                    <td width="20%" class="mtxtbold"><strong>$<?php echo number_format($monthlyi $bal2?></strong></td>
                  </tr>
                  <tr>
                    <td width="20%">Yearly</td>
                    <td width="20%"><span class="mtxtbold">$<?php echo number_format($interestcharged2?></span></td>
                    <td width="20%" class="mtxtbold"><strong>$<?php echo number_format($interestcharged $bal2?></strong></td>
                  </tr>
            </table>
    </div>


    <?php
    }
    ?>

    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Similar Threads

  1. My Calculator V4!!!
    By pointer in forum Java Help and Tutorials
    Replies: 15
    Last Post: 02-19-2009, 01:04 AM
  2. My calculator V3!!!
    By pointer in forum Java Help and Tutorials
    Replies: 0
    Last Post: 07-10-2008, 09:22 AM
  3. BH - Simple Calculator
    By BobboHobbo in forum RS3 Outdated / Broken Scripts
    Replies: 2
    Last Post: 01-30-2008, 01:56 PM
  4. xp calculator
    By jhildy in forum RS3 Outdated / Broken Scripts
    Replies: 5
    Last Post: 07-08-2007, 01:03 AM
  5. Graphing Calculator
    By Keen in forum News and General
    Replies: 16
    Last Post: 05-24-2006, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •