drsimcox Posted June 21, 2010 Share Posted June 21, 2010 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 Quote Link to comment Share on other sites More sharing options...
drsimcox Posted June 21, 2010 Author Share Posted June 21, 2010 sorry this is probably a really simple question, but im new with php and i am struggling a little bit. Any help would be greatly appreciated cheers Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 21, 2010 Share Posted June 21, 2010 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> Quote Link to comment Share on other sites More sharing options...
drsimcox Posted June 21, 2010 Author Share Posted June 21, 2010 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.