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"
"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 />
<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 />
<label>Both Score:</label>
<span><?php echo $score_both; ?></span><br />
<label>Total Score:</label>
<span><?php echo $score_totalAll; ?></span></p>
<p>
</p>
</p>
<input name="Average" type="radio" value="Average" checked="checked" />
<label>
Average</label>
<br />
<input name="Average" type="radio" value="Average" />
<label>
Total</label>
<br />
<input name="Average" type="radio" value="Average" />
<label>Both</label>
</p>
<p> </p>
<p>
<input type="submit" name="Calculate" id="Calculate" value="Calculate" />
</p>
<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>
</p>
</p>
<br /><br />
</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 page.....
<?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';
?>
I am kind of stuck as to what to do. I want to use a 'switch statement'. Would someone help me? Please?