Would someone please Direct me as to which way to go.

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?

This is the looper_test file…


Loop Tester

Loop Tester

Process Scores

      <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 />
      <input type="submit" value="Process Scores" /><br />
  <p>What would you like to do?</p>
        <input name="Average" type="radio" value="Average" checked="checked" />
        <br />
        <input name="Average" type="radio" value="Average"  />
        <br />
        <input name="Average" type="radio" value="Average"  />
        <input type="submit" name="Calculate" id="Calculate" value="Calculate" />
      <p><br />
        <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 />
<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 />

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


This is my index.php file…


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;
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.';

    // 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);

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) {
        $total += $rolls;
        $max_rolls = max($rolls, $max_rolls);
    $average_rolls = $total / $count;


include ‘loop_tester.php’;

This is the looper_test.php

NOTE: The out put should look something like this…


Sponsor our Newsletter | Privacy Policy | Terms of Service