Trouble Completing Simple Calculation PHP

Discussion in 'PHP' started by scottlpool2003, Jul 21, 2009.

  1. #1
    My aim is to perform a very simple calculation e.g.

    57
    12
    =

    The way I've written the code is to give each column a random number. E.g. 5 is one number 7 is another and the same for 1 and 2.

    But I'm a bit unsure on how to perform the calculation as it's not 57 x 12 is it.

    I wanted to tell the PHP to perform

    $variable_a (5) and $variable_b (7) to multiply by $variable_c (1) and $variable_d (2) which will give me the right answer, but I'm getting:

    Parse error: syntax error, unexpected T_VARIABLE

    Here's my code:

    <?php
    srand ((double) microtime( )*1000000);
    $random_number1 = rand(0,10);
    $random_number2 = rand(0,10);
    $random_number3 = rand(0,10);
    $random_number4 = rand(0,10);
    $random_number5 = rand(0,10);
    $random_number6 = rand(0,10);
    $random_number7 = rand(0,10);
    $random_number8 = rand(0,10);
    $random_number9 = rand(0,10);
    $random_number10 = rand(10,0);
    $random_number11 = rand(10,0);
    $random_number12 = rand(10,0);
    $random_number13 = rand(10,0);
    $random_number14 = rand(10,0);
    $random_number15 = rand(10,0);
    $random_number16 = rand(0,10);
    $random_number17 = rand(0,10);
    $random_number18 = rand(0,10);
    $random_number19 = rand(0,10);
    $random_number20 = rand(0,10);
    $random_number21 = rand(0,10);
    $random_number22 = rand(0,10);
    $random_number23 = rand(0,10);
    $random_number24 = rand(0,10);
    
    
    ?> 
    
    <?
    if($_POST['Submit']!=''){
    $variable_a = $_POST['variable_a'];
    $variable_b = $_POST['variable_b'];
    $variable_b = $_POST['variable_c'];
    $variable_b = $_POST['variable_d'];
    $ans = $_POST['ans'];
    
    
    
    
    //calculations
    
    $resulta = $variable_a $variable_b * $variable_c $variable_d;
    echo "<h1>$variable_a $variable_b *  $variable_c $variable_d; = $resulta<br></h1>";
    echo "Your answer was: $ans<br>";
    if ( $ans == $resulta ) {
    $total = $total+1;
    
    	echo "<font color=green><b>CORRECT!</b></font>";
    } 
    else {
    $total = $total+0;
    	echo "<font color=red><b>INCORRECT!</b></font>";
    }
    }
    ?>
    <table width="100" style="border:2px solid black;">
    <tr>
    <td><font color="white" size="25px">.</font><input name="variable_a" value="<?php echo "$random_number1";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number2";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr>
    <td><font size="20px">*</font><input name="variable_a" value="<?php echo "$random_number3";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number4";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr> <td colspan="3" align="center"><input name="ans" type="text" id="variable_b" ></td></tr>
    </table>
    
    <input type="submit" name="Submit" value="Calculate">
    </form>
    PHP:
    I'm getting pretty desperate as this is part of my work placement for uni and I only have until September to complete the whole website so I would very much appreciate absolutely any help with this calculation.

    Thanks in advance.
     
    scottlpool2003, Jul 21, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try this:

    $resulta = (int)($variable_a.$variable_b) * (int)($variable_c.$variable_d);

    You basically need to use a "." to concatenate the 2 numbers. You don't need the (int) but it will prevent any bad user submitted data.
     
    jestep, Jul 21, 2009 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Cheers jestep, I've done what you said and I no longer have errors. Still got a problem though, my results are coming through as incorrect.

    I'm outputting what my answer was and also outputting what the correct answer is to test it but the correct answer is always 0.

    Take a look here: http://crazyassthings.com/details.php

    <?php
    srand ((double) microtime( )*1000000);
    $random_number1 = rand(0,10);
    $random_number2 = rand(0,10);
    $random_number3 = rand(0,10);
    $random_number4 = rand(0,10);
    $random_number5 = rand(0,10);
    $random_number6 = rand(0,10);
    $random_number7 = rand(0,10);
    $random_number8 = rand(0,10);
    $random_number9 = rand(0,10);
    $random_number10 = rand(10,0);
    $random_number11 = rand(10,0);
    $random_number12 = rand(10,0);
    $random_number13 = rand(10,0);
    $random_number14 = rand(10,0);
    $random_number15 = rand(10,0);
    $random_number16 = rand(0,10);
    $random_number17 = rand(0,10);
    $random_number18 = rand(0,10);
    $random_number19 = rand(0,10);
    $random_number20 = rand(0,10);
    $random_number21 = rand(0,10);
    $random_number22 = rand(0,10);
    $random_number23 = rand(0,10);
    $random_number24 = rand(0,10);
    
    
    ?> 
    
    <?
    if($_POST['Submit']!=''){
    $variable_a = $_POST['variable_a'];
    $variable_b = $_POST['variable_b'];
    $variable_b = $_POST['variable_c'];
    $variable_b = $_POST['variable_d'];
    $ans = $_POST['ans'];
    
    
    
    
    //calculations
    
    $resulta = (int)($variable_a.$variable_b) * (int)($variable_c.$variable_d);
    echo "Your answer was: $ans<br>";
    echo "HERES WHAT THE PHP SAYS THE ANSWER IS: $resulta <BR>";
    if ( $ans == $resulta ) {
    $total = $total+1;
    
    	echo "<font color=green><b>CORRECT!</b></font>";
    } 
    else {
    $total = $total+0;
    	echo "<font color=red><b>INCORRECT!</b></font>";
    }
    }
    ?>
    <form name="calc" method="post" >
    <table width="100" style="border:2px solid black;">
    <tr>
    <td><font color="white" size="25px">.</font><input name="variable_a" value="<?php echo "$random_number1";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number2";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr>
    <td><font size="20px">*</font><input name="variable_a" value="<?php echo "$random_number3";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number4";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr> <td colspan="3" align="center"><input name="ans" type="text" id="variable_b" ></td></tr>
    </table>
    
    <input type="submit" name="Submit" value="Calculate">
    </form>
    PHP:
     
    scottlpool2003, Jul 21, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    $variable_a = $_POST['variable_a'];
    $variable_b = $_POST['variable_b'];
    $variable_b = $_POST['variable_c'];
    $variable_b = $_POST['variable_d'];

    needs to be:

    $variable_a = $_POST['variable_a'];
    $variable_b = $_POST['variable_b'];
    $variable_c = $_POST['variable_c'];
    $variable_d = $_POST['variable_d'];

    see if that fixes it.
     
    jestep, Jul 21, 2009 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Thanks for your help, I really appreciate it.

    The script still says the answer is 0.

    I thought it was because I'm adding a total to give a grade at the end but it can't be because I'm not echoing the total yet.

    <?php
    srand ((double) microtime( )*1000000);
    $random_number1 = rand(0,10);
    $random_number2 = rand(0,10);
    $random_number3 = rand(0,10);
    $random_number4 = rand(0,10);
    $random_number5 = rand(0,10);
    $random_number6 = rand(0,10);
    $random_number7 = rand(0,10);
    $random_number8 = rand(0,10);
    $random_number9 = rand(0,10);
    $random_number10 = rand(10,0);
    $random_number11 = rand(10,0);
    $random_number12 = rand(10,0);
    $random_number13 = rand(10,0);
    $random_number14 = rand(10,0);
    $random_number15 = rand(10,0);
    $random_number16 = rand(0,10);
    $random_number17 = rand(0,10);
    $random_number18 = rand(0,10);
    $random_number19 = rand(0,10);
    $random_number20 = rand(0,10);
    $random_number21 = rand(0,10);
    $random_number22 = rand(0,10);
    $random_number23 = rand(0,10);
    $random_number24 = rand(0,10);
    
    
    ?> 
    
    <?
    if($_POST['Submit']!=''){
    $variable_a = $_POST['variable_a'];
    $variable_b = $_POST['variable_b'];
    $variable_c = $_POST['variable_c'];
    $variable_d = $_POST['variable_d'];
    $ans = $_POST['ans'];
    
    
    
    
    //calculations
    
    $resulta = (int)($variable_a.$variable_b) * (int)($variable_c.$variable_d);
    echo "Your answer was: $ans<br>";
    echo "HERES WHAT THE PHP SAYS THE ANSWER IS: $resulta <BR>";
    if ( $ans == $resulta ) {
    $total = $total+1;
    
    	echo "<font color=green><b>CORRECT!</b></font>";
    } 
    else {
    $total = $total+0;
    	echo "<font color=red><b>INCORRECT!</b></font>";
    }
    }
    ?>
    <form name="calc" method="post" >
    <table width="100" style="border:2px solid black;">
    <tr>
    <td><font color="white" size="25px">.</font><input name="variable_a" value="<?php echo "$random_number1";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number2";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr>
    <td><font size="20px">*</font><input name="variable_a" value="<?php echo "$random_number3";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td>
    <td><font color="white" size="25px">.</font><input name="variable_b" value="<?php echo "$random_number4";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr>
    <tr> <td colspan="3" align="center"><input name="ans" type="text" id="variable_b" ></td></tr>
    </table>
    
    <input type="submit" name="Submit" value="Calculate">
    </form>
    PHP:
     
    scottlpool2003, Jul 21, 2009 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Check the form page. I think the input tags are named incorrectly. variable_a, variable_b, variable_a, variable_b
     
    jestep, Jul 21, 2009 IP
  7. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #7
    I made the script on http://www.onlinemovieguru.com/test.php and it works fine with the same variable names, but I'm trying to calculate it different as I need to present it differently I'm having no luck at all.
     
    scottlpool2003, Jul 21, 2009 IP
  8. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #8
    Fixed it mate, thanks for your help I forgot to change my other variable names.

    Thanks a lot for your help.
     
    scottlpool2003, Jul 21, 2009 IP