I am trying to display scores by what the user is requesting either 'Average','Total',or 'Both'. The following is my code for the loop_tester.php and below that is the code for my index.php.

I am trying to add a switch statement to the index.php file that will sets values for only the average calculation if the user selected the "average" radio button, only the total calculation if the user selected the "total" radio button, both the average and total if the user selected the "both" button.

After the user puts in the scores and then selects either 'average','total','both' it should display what the user selected.

So I am still learning how to do this but I need help starting out....would someone please look at what I have thus far and direct me into a direction of where I need to go?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Loop Tester</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
<div id="content">
    <h1>Loop Tester</h1>
    <h2>Process Scores</h2>
    <form action="." method="post">

        <p>
          <input type="hidden" name="action" value="process_scores" />

          <br /> 
          <label>Score 1:</label>
          <input type="text" name="scores[]"
               value="<?php echo $scores[0]; ?>"/><br />

          <label>Score 2:</label>
          <input type="text" name="scores[]"
               value="<?php echo $scores[1]; ?>"/><br />

          <label>Score 3:</label>
          <input type="text" name="scores[]"
               value="<?php echo $scores[2]; ?>"/><br />

          <label> </label>
          <input type="submit" value="Process Scores" /><br />


        </p>
      <p>What would you like to do?</p>
      <p>
        <label>Average</label>
            <input name="Average" type="radio" value="Average" checked="checked" />
            <br />
            <label>Total</label>
            <input name="Average" type="radio" value="Average"  />
            <br />
            <label>Both</label>
            <input name="Average" type="radio" value="Average"  />
      </p>
      <p> </p>
          <p>
            <input type="submit" name="Calculate" id="Calculate" value="Calculate" />
      </p>
          <p><br />
            <label>Scores:</label>
            <span><?php echo $scores_string; ?></span><br />

            <label>Score Total:</label>
            <span><?php echo $score_total; ?></span><br />

            <label>Average Score:</label>
            <span><?php echo $score_average; ?></span><br />
          </p>
    </form>
    <br />
    <h2>Process 1000 Die Rolls</h2>
    <form action="." method="post">
        <input type="hidden" name="action" value="process_rolls" />

        <label>Number to Roll:</label>
        <select name="number_to_roll">
            <!-- TODO: Use a for loop to display these options ! -->
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
        </select><br />

        <label> </label>
        <input type="submit" value="Process Rolls" /><br />

        <label>Maximum Rolls:</label>
        <span><?php echo $max_rolls; ?></span><br />

        <label>Average Rolls:</label>
        <span><?php echo $average_rolls; ?></span><br />

    </form>

</div>
</body>
</html>

This is my Index.php

<?php

if (isset($_POST['action'])) {
    $action =  $_POST['action'];
} else {
    $action =  'start_app';
}

switch ($action) {
    case 'start_app':
        $scores = array();
        $scores[0] = 70;
        $scores[1] = 80;
        $scores[2] = 90;
        break;
    case 'process_scores':
        $scores = $_POST['scores'];

        // validate the scores
        // TODO: Convert this if statement to a for loop
        if (empty($scores[0]) ||
            empty($scores[1]) ||
            empty($scores[2]) ||
            !is_numeric($scores[0]) ||
            !is_numeric($scores[1]) ||
            !is_numeric($scores[2])) {
                $scores_string = 'You must enter three valid numbers for scores.';
                break;
        }

        // process the scores
        // TODO: Add code that calculates the score total
        $scores_string = '';
        foreach ($scores as $s) {
            $scores_string .= $s . '|';
        }
        $scores_string = substr($scores_string, 0, strlen($scores_string)-1);

        // calculate the average
        $score_average = $score_total / count($scores);

        // format the total and average
        $score_total = number_format($score_total, 2);
        $score_average = number_format($score_average, 2);

        break;
    case 'process_rolls':
        $number_to_roll = $_POST['number_to_roll'];

        $total = 0;
        $count = 0;
        $max_rolls = -INF;

        // TODO: convert this while loop to a for loop
        while ($count < 1000) {
            $rolls = 1;
            while (mt_rand(1, 6) != 6) {
                $rolls++;
            }
            $total += $rolls;
            $count++;
            $max_rolls = max($rolls, $max_rolls);
        }
        $average_rolls = $total / $count;

        break;
}
include 'loop_tester.php';
?>

Dani AI

Generated

There are just a few, concrete fixes needed to get the behaviour you described (Average, Total, or Both):

  • the radio inputs all share the wrong name/value, so the server never sees which option was chosen;
  • the total is never calculated before you compute the average (so $score_total is undefined);
  • validation and output formatting should be done after you have numeric values; and
  • echoing raw input back into the form can cause display/XSS problems.

Below are compact examples you can drop into your form and the process_scores branch. They avoid repeating your original code but show the minimal, correct flow.

<p>What would you like to do?</p>
<label><input type="radio" name="calc" value="average" checked> Average</label>
<label><input type="radio" name="calc" value="total"> Total</label>
<label><input type="radio" name="calc" value="both"> Both</label>
// inside process_scores:
$scores = array_map('trim', $_POST['scores']);
if (count($scores) !== 3 || count(array_filter($scores, 'is_numeric')) !== 3) {
    $scores_string = 'You must enter three valid numbers for scores.';
} else {
    $nums = array_map('floatval', $scores);
    $score_total = array_sum($nums);                     // compute total first
    $score_average = $score_total / count($nums);

    $choice = $_POST['calc'] ?? 'both';
    switch ($choice) {
        case 'average':
            $score_total = '';
            $score_average = number_format($score_average, 2);
            break;
        case 'total':
            $score_average = '';
            $score_total = number_format($score_total, 2);
            break;
        default:
            $score_total = number_format($score_total, 2);
            $score_average = number_format($score_average, 2);
    }

    $scores_string = implode('|', array_map(function($v){ return htmlspecialchars($v, ENT_QUOTES); }, $scores));
}

Troubleshooting notes: use implode instead of manual substr trimming, keep only one submit button in the scores form, and always htmlspecialchars() values that are printed back into inputs. If you still see empty/odd results, add error_reporting(E_ALL); ini_set('display_errors', 1); at the top while testing, and var_dump($_POST) to confirm what the browser actually sends.

(Thanks to for the original example — the steps above address the exact gaps in the posted code so the switch can control which values are calculated and shown.)

The out put should look like this

I need the answer to this question can someone please help?

hi, please open your own thread, share your code and explain your issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.