I dont undestand... a simple thing

Discussion in 'PHP' started by danny-k, Sep 18, 2007.

  1. #1
    In a main php page I include another php page that calculates some points after some given formulas:
    page up here: ethereals.bynetics.net/testpage.php

    1. I dont undestand why id displays all options from the IF command, i have tryed with :"if", "elseif", "else" .. the same thing...

    1.1. Is there an option that my form remains with the same values after submision?


    2. As I use a Incude comand in the main page, each time i press "Calculate" it reloads the whole page, can it be done so it only reloads the include file?

    3. As i am a beginer at php I was wondering if I can use the Include like this:
    On the left side of my site I have a menu. Can I put a button that includes a php file somewere else in the page (like a button in the page content the loads a diferent php instead of footer.php that is loaded at the page load?

    I have realied that my $a variable was part of the 2v2,3v3or 5v5 string so i replaced the if with echo "$a", "v", "$a";

    Tank You,
    PS: Sorry for my enghish.

    Down here is the Calculator Code:

     
    danny-k, Sep 18, 2007 IP
  2. adrevol

    adrevol Active Member

    Messages:
    124
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
     
    adrevol, Sep 18, 2007 IP
  3. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #3
    
    <html>
    <head>
       <!--The CSS-->
       <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    <center>
    
    <form method="post">
    Rating: <input type="text" name="rating" size="4" value="1500"/>
    <table width="40" border="0">
       <tr>
          <td><input type="radio" name="team" value="2" checked>2v2</td>
          <td><input type="radio" name="team" value="3">3v3</td>
          <td><input type="radio" name="team" value="5">5v5</td>
       </tr>
    </table>
    <input type="submit" value="Calculate" />
    </form>
    
    <?php
    $a = $_POST["team"];
    $b= $_POST["rating"];
    
    if ($a == 2) {
       if ($b<="1500")
          $first_number = floor(0.70*(0.38*$b-194));
       else
          $first_number = floor(0.70*(1426.79/(1+918.836*pow(2.71828,(-0.00386405*$b)))));
    }
    
    elseif ($a == 3) {
       if ($b<="1500")
          $first_number = floor(0.80*(0.38*$b-194));
       else
          $first_number = floor(0.80*(1426.79/(1+918.836*pow(2.71828,(-0.00386405*$b)))));
    }
    
    elseif ($a == 5) {
       if ($b<="1500")
          $first_number = floor(0.38*$b-194);
       else
          $first_number = floor(1426.79/(1+918.836*pow(2.71828,(-0.00386405*$b))));
    }
    
    if(isset($a) && isset($b)) 
       echo "Your $a v $a team </br> gets for <b>$b</b> rating</br> <h4>$first_number</h4> Arena Points.";
    
    ?>
    
    </center>
    </body>
    
    </html>
    
    PHP:
    Fixed a lot of the redundancy and errors for you. As someone already said, you need == for comparison, = is just an assignment operator. This won't display the final arena points text unless you've submitted the form.
     
    omgitsfletch, Sep 18, 2007 IP
  4. danny-k

    danny-k Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Tank you, "==" must be a common beginners error... :p
    Anyway I have done the calculator in JS, and I think it works better, doesn't reload my page every calculation.
    ethereals.bynetics.net to see it at work (left side)

    I still have the questions about Include command.... can I with a Button in the content load a different php file for the footer? Like Frames work in html :confused:

    JS code:
    <HTML>
    <HEAD>
    <TITLE>Radio Button Test</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    var ap
    var t
    var r
    function testButton (form){
        for (Count = 0; Count < 3; Count++) {
            if (form.rad[Count].checked)
                break;
        }
    	t = form.rad[Count].value
    	r = form.rating.value
    if (r>1500) { ap = 1426.79 /(1+918.836*Math.pow(2.71828,(-0.00386405*r)));}
    else { ap = 0.38*r-194;}
    if (t==2) {ap= Math.floor(0.7*ap);}
    if (t==3) {ap= Math.floor(0.8*ap);}
    if (t==5) {ap= Math.floor(ap);}
    alert ("Your " + t + "v" + t + " team gets for " + r + " rating " + ap + " Arena Points");
    }
    </SCRIPT>
    </BODY>
    <FORM NAME="testform">
    <INPUT type="text" NAME="rating" size="4" value="1500"/><BR>
    <table width="40" border="0">
    <tr>
    <td><INPUT TYPE="radio" NAME="rad" Value="2" onClick=0 checked>
    2v2</td>
    <td><INPUT TYPE="radio" NAME="rad" Value="3" onClick=0>
    3v3</td>
    <td><INPUT TYPE="radio" NAME="rad" Value="5" onClick=0>
    5v5 </td>
    </tr>
    </table>
    <BR>
    <INPUT TYPE="button" NAME="button" Value="Calculate" 
        onClick="testButton(this.form)"> <BR>
    </FORM>
    </HTML>
    PHP:
     
    danny-k, Sep 18, 2007 IP
  5. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Hmm, you could have a button set a variable, and then have your footer file depend on the value of that variable. The only thing is since it's PHP, it won't be instantaneous. You need to reload the page to get that information. This is a fundamental part of the way PHP runs, it's more of a scripting language. The only ways I can think to do things like that on the fly are Javascript and AJAX.
     
    omgitsfletch, Sep 18, 2007 IP