0

Now I am struggling with how to declare the average variable and have it calculate my average. I commented out some of the things I have tried.

<?php
    // get the data from the form
    $first = filter_input(INPUT_POST, 'first');
    $last = filter_input(INPUT_POST, 'last');
    $one = filter_input(INPUT_POST, 'one',
        FILTER_VALIDATE_FLOAT);
    $two = filter_input(INPUT_POST, 'two',
        FILTER_VALIDATE_FLOAT);
    $three = filter_input(INPUT_POST, 'three',
        FILTER_VALIDATE_FLOAT);
    //$Average = float ($one + $two + $three)/3;
    

    // validate Score one
    if ($one === FALSE ) {
        $error_message = 'Score one must be a valid number.'; 
    } else if ( $one < 0 ) {
        $error_message = 'Score one cannot be less than zero.'; 
   // validate Score two
    if ($two === FALSE ) {
        $error_message = 'Score two must be a valid number.'; 
    } else if ( $two < 0 ) {
        $error_message = 'Score two cannot be less than zero.'; 
    // validate Score three
    if ($three === FALSE ) {
        $error_message = 'Score three must be a valid number.'; 
    } else if ( $three < 0 ) {
        $error_message = 'Score three cannot be less than zero.'; 
    // set error message to empty string if no invalid entries
    } else {
        $error_message = ''; }

    // if an error message exists, go to the index page
    if ($error_message != '') {
        include('index.php');
        exit();
    }
    
    // calculate the average score
        //$Sum = $one; + $two; + $three;
        //$Average = $Sum / 3;}}
        $Average = ($one + $two + $three)/3;}}
        //$Average =  ($one + $two + $three)/3};
        $Average = number_format($Average, 2);
    
?>
<!DOCTYPE html>
<html>
<head>
    <title>Assignment 2</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
    <main>
        <h1>Assignment 2</h1>

        <label>Student Name:</label>
        <span><?php echo $first; ?></span><span><?php echo " ",  $last; ?></span><br />

        <label>Your Scores:</label>
        <span><?php echo $one, ","; ?></span> <span><?php echo $two, ","; ?></span> <span><?php echo $three; ?></span><br />

        <label>Average:</label>
        <span><?php echo $Average; ?></span><br />
        
    </main>
</body>
</html>

I have some type of syntax error I can't find. I was thinking it was or something like that but comparing to my sample code from class I can't see what I am missing. Is it jumping out to anyone else?

I have reviewed the brackets and I'm not seeing what I am doing incorrectly. I have compared it to my classes source code and am scratching my head. I used a validator and it isn't showing any errors.

<?php
    // get the data from the form
    $one = filter_input(INPUT_POST, 'one',
        FILTER_VALIDATE_FLOAT);
    $two = filter_input(INPUT_POST, 'two',
        FILTER_VALIDATE_FLOAT);
    $three = filter_input(INPUT_POST, 'three',
        FILTER_VALIDATE_INT);

    // validate Score one
    if ($one === FALSE ) {
        $error_message = 'Score one must be a valid number.'; 
    } else if ( $one < 0 ) {
        $error_message = 'Score one cannot be less than zero.'; 
   // validate Score two
    if ($two === FALSE ) {
        $error_message = 'Score two must be a valid number.'; 
    } else if ( $two < 0 ) {
        $error_message = 'Score two cannot be less than zero.'; 
    // validate Score three
    if ($three === FALSE ) {
        $error_message = 'Score three must be a valid number.'; 
    } else if ( $three < 0 ) {
        $error_message = 'Score three cannot be less than zero.'; 
    // set error message to empty string if no invalid entries
    } else {
        $error_message = ''; }

    // if an error message exists, go to the index page
    if ($error_message != '') {
        include('index.php');
        exit();
    }
    
    // calculate the average score
    $Average = ($one + $two + $three) / 3;}
    
?>
<!DOCTYPE html>
<html>
<head>
    <title>Assignment 2</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
    <main>
        <h1>Assignment 2</h1>

        <label>Student Name:</label>
        <span><?php echo ($first + $last); ?></span><br />

        <label>Your Scores:</label>
        <span><?php echo $one; $two; $three; ?></span><br />

        <label>Average:</label>
        <span><?php echo number_format($Average,2); ?></span><br />
        
    </main>
</body>
</html>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jwaldon
  • 45
  • 6
  • change `3;}` to `3;}}` then find/use a better editor which shows syntax errors – Lawrence Cherone Aug 22 '20 at 15:30
  • Thank you! That helped. I am still getting errors but ones I can dig into more. What Validator do you recommend? I use CSE HTML Validator. Ther personal user version. – Jwaldon Aug 22 '20 at 15:34
  • @LawrenceCherone hate to bug you again, but do you know where my math is off? I feel like the average should be really simple but I keep getting 0.00 for my answer regardless of my input. – Jwaldon Aug 22 '20 at 16:02

0 Answers0