Inersha
08-01-2008, 11:24 AM
I'm new to PHP and just learning the basics. I've written a 'risk of ruin' calculator that basically uses a form and an equation to give a percentage value based on 3 input numbers. However, I have 2 problems...
1) I think I'm going to the long way around things, and using more code than neccesary.
2) I can't think of how to only make the form process after you enter some numbers and click 'submit'. I'm sure I must use some sort of 'if' statement, as I only want the form to be processed after numbers have been entered and the user has clicked 'calculate'.
Here is the code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<strong>Bankroll: </strong><input type="text" name="br" /><br/>
<strong>Win Rate: </strong><input type="text" name="wr" /><br/>
<strong>Standard Deviation: </strong><input type="text" name="sd" /><br/>
<input type="submit" value="Calculate" />
</form>
<?php
$xa = $_POST["br"];
$ya = $_POST["wr"];
$za = $_POST["sd"];
$x = preg_replace("/[^0-9]/",'', $xa);
$y = preg_replace("/[^0-9]/",'', $ya);
$z = preg_replace("/[^0-9]/",'', $za);
$xy2 = -2*$x*$y;
$zz = $z*$z;
$all = $xy2/$zz;
$answer = exp($all);
$raw_ror = 100*$answer;
$ror = number_format($raw_ror,2);
echo "<strong>Risk Of Ruin:</strong> $ror%"; }
?>
I especially feel as though I'm going the long way around things when it comes to repeating preg_replace for each number that gets submitted. I was also careful with the calculation to make sure I did it in stages and got it correct, but it does look a bit long winded too.
Any advice on getting the form to process after submission only and tips on how to code more cleanly would be much appreciated. Thanks.
1) I think I'm going to the long way around things, and using more code than neccesary.
2) I can't think of how to only make the form process after you enter some numbers and click 'submit'. I'm sure I must use some sort of 'if' statement, as I only want the form to be processed after numbers have been entered and the user has clicked 'calculate'.
Here is the code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<strong>Bankroll: </strong><input type="text" name="br" /><br/>
<strong>Win Rate: </strong><input type="text" name="wr" /><br/>
<strong>Standard Deviation: </strong><input type="text" name="sd" /><br/>
<input type="submit" value="Calculate" />
</form>
<?php
$xa = $_POST["br"];
$ya = $_POST["wr"];
$za = $_POST["sd"];
$x = preg_replace("/[^0-9]/",'', $xa);
$y = preg_replace("/[^0-9]/",'', $ya);
$z = preg_replace("/[^0-9]/",'', $za);
$xy2 = -2*$x*$y;
$zz = $z*$z;
$all = $xy2/$zz;
$answer = exp($all);
$raw_ror = 100*$answer;
$ror = number_format($raw_ror,2);
echo "<strong>Risk Of Ruin:</strong> $ror%"; }
?>
I especially feel as though I'm going the long way around things when it comes to repeating preg_replace for each number that gets submitted. I was also careful with the calculation to make sure I did it in stages and got it correct, but it does look a bit long winded too.
Any advice on getting the form to process after submission only and tips on how to code more cleanly would be much appreciated. Thanks.