Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2008, 11:24 AM   PM User | #1
Inersha
New Coder

 
Join Date: Jun 2008
Posts: 50
Thanks: 18
Thanked 0 Times in 0 Posts
Inersha is on a distinguished road
Basic PHP Help

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:

PHP 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.
Inersha is offline   Reply With Quote
Old 08-01-2008, 11:35 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Try something like
PHP Code:
<?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
 
}
}
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 08-01-2008 at 11:37 AM..
abduraooft is offline   Reply With Quote
Old 08-01-2008, 11:48 AM   PM User | #3
Inersha
New Coder

 
Join Date: Jun 2008
Posts: 50
Thanks: 18
Thanked 0 Times in 0 Posts
Inersha is on a distinguished road
There seems to be a problem with this line:

PHP Code:
if(empty($xa $_POST["br"])) 
It comes up with the error:

Code:
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.
Inersha is offline   Reply With Quote
Old 08-01-2008, 11:53 AM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Sorry.. I hadn't tested
PHP Code:
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).
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 08-01-2008 at 11:57 AM..
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
Inersha (08-01-2008)
Old 08-01-2008, 12:03 PM   PM User | #5
Inersha
New Coder

 
Join Date: Jun 2008
Posts: 50
Thanks: 18
Thanked 0 Times in 0 Posts
Inersha is on a distinguished road
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?
Inersha is offline   Reply With Quote
Old 08-01-2008, 12:18 PM   PM User | #6
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
You could combine/reduce
Quote:
$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
PHP Code:
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.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:30 PM.


Advertisement
Log in to turn off these ads.