Jump to content

Price comparison calculator


gsvavarsson

Recommended Posts

hi, i'm new PHP, so I need your help.

I modified a calculator to my conditions but I want the result to overwrite the input boxes and submit btn.

 

here is the code:

 

ps. don't mind the Danish, i don't :D

<?php 

$page = $_GET['page']; 

class calc { 
     var $number1; 
     var $number2; 

          function add($number1,$number2) 
          { 
                   $result =$number1-($number2*0.98); 
                    echo("Du kommer til med ad spare $result dkk<br>"); 
                    exit; 
           } 
} 
$calc = new calc(); 
?> 
<TITLE>PHP Calculator v1</TITLE> 
<form name="calc" action="?page=calc" method="POST"> 
Nuværende månedlig udgift: <input type=text name=value1><br> 
Forbrug af kopper/md.: <input typ e=text name=value2><br> 
<input type=submit value="Calculate"> 
</form> 
<?php 
if($page == "calc"){ 
$number1 = $_POST['value1']; 
$number2 = $_POST['value2']; 
$oper = $_POST['oper']; 
     if(!$number1){ 
          echo("Du skal tast ind nuværende måndlig udgift!"); 
          exit; 
     } 
     if(!$number2){ 
          echo("Du skal tast ind Forbrug p/md.!"); 
          exit; 
     } 
     /*if(!$oper){ 
          echo("You must select an operation to do with the numbers!"); 
          exit; 
     } */
     if(!eregi("[0-9]", $number1)){ 
          echo("De skal være nummer!"); 
          exit; 
     } 
     if(!eregi("[0-9]", $number2)){ 
          echo("De skal være nummers!"); 
          exit; 
     } 
 if($number2<299) {
	 echo("Månedlig forbrug skal være 300 eller mere");
 }
     else{ 
          $calc->add($number1,$number2); 
     } 
} 
?>

 

mvh

Link to comment
Share on other sites

The way I've been handling this is to actually put html in  a function, and call that function to return the html with the new values.  There might be a better way.

 

switch ($action){
	case 'step1':
		echo page_update_form($result);
		break;

function page_update_form($result){
global $config;

$ret.='
<form>
<table>


</table>
</form>';
return $ret;
}

Link to comment
Share on other sites

In my opinion, the structure of your page really needs a makeover. You have a class that does nothing but a simple calculation. You then have a lot of error handling outside the class. If that is all your class is going to do, I wouldn't even use a class. But, you could move the validations into the class to make it more useful. Also, your error handling is not very elegant since it uses exit(). Instead you should always display a complete page.

 

Also, your class defines two properties for the class - but never uses them. The variables you use in the add() method are not the same as the properties you defined for the class.

 

I have made some major modifications below. I think it is all correct - but since the code has no comments and the output is not english I'm not entirely sure.

 

<?php 

class calc
{
    var $number1 = ''; 
    var $number2 = '';
    var $errors = array();
    var $response = '';

    function add($value1, $value2) 
    {
        $this->set_number1($value1);
        $this->set_number2($value2);

        if(count($this->errors))
        {
            $this->response  = "The following errors occured:\n";
            $this->response .= "<ul>\n";
            foreach($this->errors as $error)
            {
                $this->response .= "<li>{$error}</li>\n";
            }
            $this->response .= "</ul>\n";
            return false;
        }
        else
        {
            $result = $this->number1 - ($this->number2*0.98);
            $this->response ="Du kommer til med ad spare $result dkk<br>";
            return true;
        }
    }

    function set_number1($value)
    {
        $this->number1 = trim($value);
        if(empty($this->number1))
        {
            $this->errors[] = "Du skal tast ind nuværende måndlig udgift!"; 
        }
        elseif(!ctype_digit($this->number1))
        {
            $this->errors[] = "De skal være nummer!";
        }
        return;
    }

    function set_number2($value)
    {
        $this->number2 = trim($value);
        if(empty($this->number2))
        {
            $this->errors[] = "Du skal tast ind Forbrug p/md.!"; 
        }
        elseif(!ctype_digit($this->number2))
        {
            $this->errors[] = "De skal være nummer!";
        }
        elseif($this->number2>299)
        {
            $this->errors[] = "Månedlig forbrug skal være 300 eller mere";
        }
        return;
    }
} 

$calc = new calc();
if(isset($_POST['value1']) && isset($_POST['value2']))
{ 
    $calc->add(($_POST['value1'], $_POST['value2']);
}

?>
<html>
<head>
<title>PHP Calculator v1</title>
</head>

<body>
<form name="calc" action="" method="post"> 
    Nuværende månedlig udgift: <input type=text name="value1" value="<?php echo $calc->number1; ?>"><br> 
    Forbrug af kopper/md.: <input type=text name="value2" value="<?php echo $calc->number2; ?>"><br> 
    <input type=submit value="Calculate"><br><br>
    <?php echo $calc->response; ?>
</form> 
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.