Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Basic PHP Help

  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    50
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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
     
    }
    }
    Last edited by abduraooft; 08-01-2008 at 12:37 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    50
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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).
    Last edited by abduraooft; 08-01-2008 at 12:57 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Inersha (08-01-2008)

  • #5
    New Coder
    Join Date
    Jun 2008
    Posts
    50
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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
    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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •