0

Morning Everyone,

I am trying to troubleshoot this code. I have made progress, but I am doing a simple average calculation. When I try and declare a variable before using it I have issues. When I try to just use it I get errors saying declare a variable.

Any idea what I am doing incorrectly on the $Average portion. I either can't render the page or I get 0.00 because when I declare the variable I don't have an input for it yet.

<?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);
    //var = $Average ;
    $Sum = filter_input(INPUT_POST, 'Sum');

    // 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 = 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>

2
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your own code. Take a quick look at a coding standard for your own benefit. Commented Aug 22, 2020 at 17:42
  • Hey there! Your code looks pretty long. It would help answerers if you could trim it down to only the code causing the error. Commented Aug 23, 2020 at 1:02

1 Answer 1

1

In your if else codes , your values can't arrive calculation line. I edited this part and include isset for $one , $two , $three :

<?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);
    //var = $Average ;
    $Sum = filter_input(INPUT_POST, 'Sum');
    
            if(!isset($one)){$one=0;}else{$error_message ='You must enter a value';}
            if(!isset($two)){$two=0;}else{$error_message ='You must enter a value';}
            if(!isset($three)){$three=0;}else{$error_message ='You must enter a value';}

    // 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 = 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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. So I was focusing on how could I be messing up my average calculation when really the way my errors were being handled was preventing it from calculating. Thank you again for providing something more helpful than suggesting I adjust my spacing and figure it out. :-) I am trying to learn and your answer really helped me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.