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

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

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

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

          <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 />
          <label>Both Score:</label>
          <span><?php echo $score_both; ?></span><br />
          <label>Total Score:</label>
        <span><?php echo $score_totalAll; ?></span></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"  />
      <p>       </p>
        <input type="submit" name="Calculate" id="Calculate" value="Calculate" />
      <p><br />
        <label>Average Score:</label>
        <span><?php echo $score_average; ?></span><br />
        <label>Both Score:</label>
        <span><?php echo $score_both; ?></span><br />
        <label>Total Score:</label>
        <span><?php echo $score_totalAll; ?></span>
      <br /><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 />

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



This is my index.php page.....


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

I am kind of stuck as to what to do. I want to use a 'switch statement'. Would someone help me? Please?

3 Years
Discussion Span
Last Post by hag++

Thank you for the nicely indented post. I am confused as to where you are stuck. Can you point out the exact line numbers and reason why you are stuck?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.