...

View Full Version : Basic PHP Help



Inersha
08-01-2008, 12:24 PM
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.

abduraooft
08-01-2008, 12:35 PM
Try something like


<?php
if(isset($_POST["br"])){
$errors=array();
if(empty($xa = $_POST["br"]))
$errors[]="field-1 is empty";
................
if(count($errors)>0)
foreach($errors as $error)
echo $error .'<br/>';
else{
// do the rest of calculation
}
}

Inersha
08-01-2008, 12:48 PM
There seems to be a problem with this line:


if(empty($xa = $_POST["br"]))

It comes up with the error:


Parse error: syntax error, unexpected '=', expecting ')'

Could you give me a brief explanation on how the code works too? Feel like a cheat if I use some code and don't know what is going on!

Thanks again.

abduraooft
08-01-2008, 12:53 PM
Sorry.. I hadn't tested


if(isset($_POST["br"])){ //ensures there is some submitted data
$errors=array();
// ensures all the required fields are not empty;
if(empty($_POST["br"]))
$errors[]="field-1 is empty";
if(empty($_POST["wr"]))
$errors[]="field-2 is empty";
if(empty($_POST["sd"]))
$errors[]="field-3 is empty";

if(count($errors)>0)
foreach($errors as $error)
echo $error .'<br/>';
else{
$xa = $_POST["br"];
$ya = $_POST["wr"];
$za = $_POST["sd"];
// do the rest of calculation
}
}

PS: You may have to add some more checks(ensure values of the variables are numbers).

Inersha
08-01-2008, 01:03 PM
Thankyou, works perfectly! I'll have a look to see if I can figure out how it works.

On a side note, how is my coding? Am I going the long way around things or does everything look fairly standard so far?

abduraooft
08-01-2008, 01:18 PM
You could combine/reduce

$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); by


foreach($_POST as $key=>$value){
$$key=preg_replace("/[^0-9]/",'', $value);
}

echo $br.', '.$wr.', '.$sd; // for testing

Thus if you use the names of your input text fields as x,y,z instead of br,wr,sd(or just use these variables in calculations) then you can avoid usage of intermediate variables also.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum