Jump to content

Selection with Radio Buttons using a Switch statement


dlmagers

Recommended Posts

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? PLEASE HELP!

 

NOTE:  The output should look like this..... http://wcet2.waketech.edu/wrconway/web182/lesson7/ch08_ex2/ 

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

And this is what I have got for my index.html

<?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';
?>
Edited by dlmagers
Link to comment
Share on other sites

To know which radio was selected, each option needs to have it's own value. The code currently has all the values set as "Average".

 <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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.