-1

This program is supposed to accept scores for 20 assignments, store them in an array, and then echo two things: the number of assignments for which a score was entered, and the total of all the scores. My issue is that no matter whether or not I enter a value for an assignment, php not only echos a label for the assignment, but it counts the empty field as part of the array and adds it to the variable I'm using to store the number of assignments for which a score has been entered. The below is the page on which the user enters their score(s), and the page below that is the page that is supposed to display their results.


    <!DOCTYPE html>
    <html>
    
    <head>
        <title>Assignments</title>
        <link href="main.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <div id="content">
            <h1>Enter Your Scores (0-100, 2000 possible)</h1></br>
        <p>Press The "Calculate" button when finished.</p></br>
        <form action="arraysolo.php" method="post">
            <div id="data">
                <label>Assignment 1</label>
            <input type="text" name="assign1"><br />
                <label>Assignment 2</label>
            <input type="text" name="assign2"><br />
                <label>Assignment 3</label>
            <input type="text" name="assign3"><br />
                <label>Assignment 4</label>
            <input type="text" name="assign4"><br />
                <label>Assignment 5</label>
            <input type="text" name="assign5"><br />
                <label>Assignment 6</label>
            <input type="text" name="assign6"><br />
                <label>Assignment 7</label>
            <input type="text" name="assign7"><br />
                <label>Assignment 8</label>
            <input type="text" name="assign8"><br />
                <label>Assignment 9</label>
            <input type="text" name="assign9"><br />
                <label>Assignment 10</label>
            <input type="text" name="assign10"><br />
                <label>Assignment 11</label>
            <input type="text" name="assign11"><br />
                <label>Assignment 12</label>
            <input type="text" name="assign12"><br />
                <label>Assignment 13</label>
            <input type="text" name="assign13"><br />
                <label>Assignment 14</label>
            <input type="text" name="assign14"><br />
                <label>Assignment 15</label>
            <input type="text" name="assign15"><br />
                <label>Assignment 16</label>
            <input type="text" name="assign16"><br />
                <label>Assignment 17</label>
            <input type="text" name="assign17"><br />
                <label>Assignment 18</label>
            <input type="text" name="assign18"><br />
                <label>Assignment 19</label>
            <input type="text" name="assign19"><br />
                <label>Assignment 20</label>
            <input type="text" name="assign20"><br />
        </div>
            <div id="button">
                <label>&nbsp </label>
                <input type="submit" value="Calculate" /><br />
            </div>
        </form>
        </div>
    </body>
    </html>

The below is the page which displays the users results.


    <!DOCTYPE html>
    <html>
    
    <head>
        <title>Your Results:</title>
        <link href="main.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <div id="content">
        
            <h1>Score Per Assignment:</h1></br>
    <?php 
    $score = array();
     if(!isset($_POST['assign1'])) {
          $score[0] = null;
        }else {
            $score[0] = $_POST['assign1'];
         echo "<h2>Assignment 1:</h2>" . $score[0] . "</br>"; 
        }
        
        if(!isset($_POST['assign2'])) {
          $score[1] = null;
        }else {
            $score[1] = $_POST['assign2'];
            echo "<h2>Assignment 2:</h2>" . $score[1] . "</br>";
        }
        
        if(!isset($_POST['assign3'])) {
          $score[2] = null;
        }else {
            $score[2] = $_POST['assign3'];
            echo "<h2>Assignment 3:</h2>" . $score[2] . "</br>";
        }
        
        if(!isset($_POST['assign4'])) {
          $score[3] = null;
        }else {
            $score[3] = $_POST['assign4'];
            echo "<h2>Assignment 4:</h2>" . $score[3] . "</br>";
        }
        
        if(!isset($_POST['assign5'])) {
          $score[4] = null;
        }else {
            $score[4] = $_POST['assign5'];
            echo "<h2>Assignment 5:</h2>" . $score[4] . "</br>";
        }
        
     if(!isset($_POST['assign6'])) {
          $score[5] = null;
        }else {
            $score[5] = $_POST['assign6'];
         echo "<h2>Assignment 6:</h2>" . $score[5] . "</br>";
        }
        
        if(!isset($_POST['assign7'])) {
          $score[6] = null;
        }else {
            $score[6] = $_POST['assign7'];
            echo "<h2>Assignment 7:</h2>" . $score[6] . "</br>";
        }
        
        if(!isset($_POST['assign8'])) {
          $score[7] = null;
        }else {
            $score[7] = $_POST['assign8'];
            echo "<h2>Assignment 8:</h2>" . $score[7] . "</br>";
        }
        
        if(!isset($_POST['assign9'])) {
          $score[8] = null;
        }else {
            $score[8] = $_POST['assign9'];
            echo "<h2>Assignment 9:</h2>" . $score[8] . "</br>";
        }
        
        if(!isset($_POST['assign10'])) {
          $score[9] = null;
        }else {
            $score[9] = $_POST['assign10'];
            echo "<h2>Assignment 10:</h2>" . $score[9] . "</br>";
        }
        
             if(!isset($_POST['assign11'])) {
          $score[10] = null;
        }else {
            $score[10] = $_POST['assign11'];
                 echo "<h2>Assignment 11:</h2>" . $score[10] . "</br>";
        }
        
        if(!isset($_POST['assign12'])) {
          $score[11] = null;
        }else {
            $score[11] = $_POST['assign12'];
            echo "<h2>Assignment 12:</h2>" . $score[11] . "</br>";
        }
        
        if(!isset($_POST['assign13'])) {
          $score[12] = null;
        }else {
            $score[12] = $_POST['assign13'];
            echo "<h2>Assignment 13:</h2>" . $score[12] . "</br>";
        }
        
        if(!isset($_POST['assign14'])) {
          $score[13] = null;
        }else {
            $score[13] = $_POST['assign14'];
            echo "<h2>Assignment 14:</h2>" . $score[13] . "</br>";
        }
        
        if(!isset($_POST['assign15'])) {
          $score[14] = null;
        }else {
            $score[14] = $_POST['assign15'];
            echo "<h2>Assignment 15:</h2>" . $score[14] . "</br>";
        }
        
             if(!isset($_POST['assign16'])) {
          $score[15] = null;
        }else {
            $score[15] = $_POST['assign16'];
                 echo "<h2>Assignment 16:</h2>" . $score[15] . "</br>";
        }
        
        if(!isset($_POST['assign17'])) {
          $score[16] = null;
        }else {
            $score[16] = $_POST['assign17'];
            echo "<h2>Assignment 17:</h2>" . $score[16] . "</br>";
        }
        
        if(!isset($_POST['assign18'])) {
          $score[17] = null;
        }else {
            $score[17] = $_POST['assign18'];
            echo "<h2>Assignment 18:</h2>" . $score[17] . "</br>";
        }
        
        if(!isset($_POST['assign19'])) {
          $score[18] = null;
        }else {
            $score[18] = $_POST['assign19'];
            echo "<h2>Assignment 19:</h2>" . $score[18] . "</br>";
        }
        
        if(!isset($_POST['assign20'])) {
          $score[19] = null;
        }else {
            $score[19] = $_POST['assign20'];
            echo "<h2>Assignment 20:</h2>" . $score[19] . "</br>";
        }
     ?>
    <?php
    $sum = array_sum($score); 
    $num_elements = count($score); 
    
    echo "<h2>Assignments Completed:</h2>" . $num_elements . 
    "</br>";
    echo "<h2>Assignments Score Total:</h2>" . $sum . 
    "</br>";
            
            
    ?>
    
    </div>
    </body>
    </html>

 
wno7736
  • 1
  • 2
  • 2
    Step 1) You've got 20 nearly identical code blocks, repeated for 20 nearly identical variables. Instead, make one code block in a loop that iterates over the values in the array. – Alex Howansky Apr 18 '21 at 17:51
  • 2
    Step 2) Before the loop, remove empty values from the array. You could `unset` them or use something like `array_filter()`. – Alex Howansky Apr 18 '21 at 17:52
  • Thank you for the prompt reply! Would you be able to tell me what the loop to avoid those twenty statements would look like? – wno7736 Apr 18 '21 at 20:30
  • Also, it is common practice to relate your inputs with their label. See some demonstrations of `for` and `id` here: https://stackoverflow.com/q/774054/2943403 – mickmackusa Apr 18 '21 at 22:29

1 Answers1

0

To expand on Alex Howansky's answer, you'll need to turn your <input> fields into an array (see How do I create arrays in a HTML form? in PHP's manual).

To do that, just replace all input fields with:

<input type="text" name="assign[]"><br />

Afterwards, you can loop them like so (this would replace your PHP code in arraysolo.php):

$score = [];

foreach ($_POST['assign'] ?? [] as $key => $value) {

    $is_valid = is_numeric($value) && $value >= 0 && $value <= 200; 

    if ($is_valid) {

        $score[] = $value;
        echo "<h2>Assignment " . ($key + 1) . ":</h2>" . $value . "</br>";
    }
}

$sum = array_sum($score); 
$num_elements = count($score); 

echo "<h2>Assignments Completed:</h2>" . $num_elements . "</br>";
echo "<h2>Assignments Score Total:</h2>" . $sum . "</br>";

This way, you'll also don't have to filter for any null values (because you won't assign them in the first place).

nosurs
  • 680
  • 6
  • 13