Jump to content

Function not working


Go to solution Solved by kicken,

Recommended Posts

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

  • Solution

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