Jump to content

price calculation using functions


drsimcox

Recommended Posts

i am trying to create a basic php page that calculates the the a the area of a room (length * width) then multiply's the area by a set price based on which quality carpet is selected.

 

here is what i have:

<html>
<head><title>calculation</title>
<style>
body {
background-color:#D8D8D8;
color:#09F;
}
</style>
</head>
<body>
<?php
$quality = $_POST['quality'] ;
$width = $_POST['width'] ;
$length = $_POST['length'] ;

if ($quality == "best") {
	function best_cost() {
		echo $price;
	}
}
else {
	function extra_cost() {
		echo $price;
	}
}

best_cost($price = ($width * $length) * 119.95);
extra_cost($price = ($width * $length) * 79.95);
?>
</body>
</html>

 

i cant figure out why both functions 'best_cost' and 'extra_cost' dont pass back the variable $price.

do i perhaps need to pass the variables 'length' and 'width' when i first call the functions?

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/205430-price-calculation-using-functions/
Share on other sites

Your usage of functions is completely wrong. You are defining the functions in the if/else statements - not calling them. And, those functions are only defined to echo $price. The lines at teh bottom for best_cost() and extra_cost() would do nothing - except maybe produce errors. Try this:

<?php

function calculatePrice($area, $quality)
{
    switch($quality)
    {
        case: 'best':
            $price_psf = 119.95;
            break;
        default:
            $price_psf = 79.95;
            break;
    }
    $total = $area * $price_psf;
    return $total;
}

$quality = $_POST['quality'];
$width   = (int) $_POST['width'];
$length  = (int) $_POST['length'];
$area = $width * $length;
$total = calculatePrice($area, $quality);
?>
<html>
<head><title>calculation</title>
<style>
body {
   background-color:#D8D8D8;
   color:#09F;
}
</style>
</head>
<body>
Width: <?php echo $width; ?><br />
Length: <?php echo $length; ?><br />
Price per sq. foot: <?php echo $price_psf; ?><br />
Total Price: <?php echo $total; ?><br />
</body>
</html>

mjdamato,

thank you for your reply, i can see where i was going wrong. I am still having some problems though.

I enter the details into my form and click submit and it loads the calculation.php page, but for some reason the page is just blank?

perhaps there is something wrong with my form as well?

 

  <form action="calculation.php" method="post">
   <p> Select a carpet grade: 
    <input type="radio" name="quality" value="best" />Best Quality
    <input type="radio" name="quality" value="extra" />Extra Value
    </p>
    <p>
    Enter room width in metres:<input type="text" size="6" maxlength="6" name="width" />
    Enter room length in metres:<input type="text" size="6" maxlength="6" name="length" />
    </p>
    <input type="submit" value="Click To Submit" />
<input type="reset" value="erase and Restart" />
  </form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.