andy1212 Posted July 23, 2013 Share Posted July 23, 2013 So I'm trying to test out a calculator just to play around with php and I can't seem to get the calculator to do any math. When I test out just adding, subtracting, multiplying numbers like this, $test = 1+2*4-5; and echo $test it shows the result which is 7. But when I make a form and allow myself to enter in numbers, it's like it doesn't recognize the variables as numbers. Here's the code I have, <? if (isset($_POST['item_name'], $_POST['item_price'], $_POST['item_age'], $_POST['conditiondrop'])) { $item_name = $_POST['item_name']; $item_price = $_POST['item_price']; $item_age = $_POST['item_age']; $condition = $_POST['conditiondrop']; $errors = array(); $value1 = intval(strval($item_age*0.1)); if ($condition == 'excellent') { $value = intval(strval($item_price-$value1)); } if ($condition == 'good') { $value = intval(strval($item_price-$item_price*0.05+$value1)); } if ($condition == 'ok') { $value = intval(strval($item_price-$item_price*0.1+$value1)); } if ($condition == 'needstlc') { $value = intval(strval($item_price-$item_price*0.2+$value1)); } } ?> Cost Calc: <form action="" method="post"> item name:<br /> <input type="text" name="item_name" /><br /> item purchase price:<br /> <input type="text" name="item_price" /><br /> item age:<br /> <input type="text" name="item_age" /><br /> item condition:<br /> <select name="conditiondrop"> <option name="excellent">excellent condition</option> <option name="good">good condition</option> <option name="ok">ok condition</option> <option name="needstlc">needs some tlc</option> </select><br /> <input type="submit" /> </form><br /><br /> <? echo $item_name.'<br />'. $condition.'<br />'. $value1.'<br />'. $value ; ?> So I can echo out item_name and condition fine but value1 and value don't get echoed out. Thanks for your time and help in advance. Link to comment https://forums.phpfreaks.com/topic/280409-function-not-working/ Share on other sites More sharing options...
kicken Posted July 23, 2013 Share Posted July 23, 2013 Your use of intval(strval( is a bit odd, and completely unnecessary. What values are you entering into your text boxes for item_price and item_age? Are you including any non-numeric symbols (ie, $, comma)? PHP will convert the values to a number for the math automatically, but if they contain those extra symbols the conversion may fail and you'd get unexpected results. Use var_dump when outputting your values for debugging so you can see exactly what they contain, a simple echo does not always tell the whole story. Rather than make the value of $condition a string which you then compare against to change the equation, you could just make it's value the percentage off and use it directly in the equation. ie: <select name="conditiondrop"> <option name="0">excellent condition</option> <option name="0.05">good condition</option> <option name="0.1">ok condition</option> <option name="0.2">needs some tlc</option> </select> $value = $item_price-$item_price*$condition+$value1; Bonus math tip: $item_price - $item_price*0.1 could be written more simply as: $item_price*0.9 Taking 10% off of something, is the same as taking 90% of it's value. Link to comment https://forums.phpfreaks.com/topic/280409-function-not-working/#findComment-1441746 Share on other sites More sharing options...
andy1212 Posted July 23, 2013 Author Share Posted July 23, 2013 ok thanks Link to comment https://forums.phpfreaks.com/topic/280409-function-not-working/#findComment-1441751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.