PHP error messages

I’m taking php/SQL course and for the life of me I can’t figure out why I’m getting “Undefined variable” errors when I run the code. All errors are on the loop_tester.php files - lines 32, 35, 38, 60 and 63. There are 2 files index.php and loop_tester.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">
        <input type="hidden" name="action" value="process_scores" />

        <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>&nbsp;</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 />
    </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">
        
        <?php // for loop to display option 
        for ($i = 1; $i < 7; $i++) : ?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        <?php endfor; ?>
        </select><br />

        <label>&nbsp;</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>

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

    // Q6 - for loop to validate user entries
    $is_valid = true;
    for ($i = 0; $i < count($scores); $i++) {
        if (empty($scores[$i]) || !is_numeric($scores[$i])) {
            $scores_string = 'You must enter three valid numbers.';
            $is_valid = false;
            break;
        }
    }
    if (!$is_valid) {
        break;
    }
    // Q5 - calculate total and average
    $scores_string = '';
    $score_total = 0;
    foreach ($scores as $s) {
        $scores_string .= $s . '|';
        $score_total += $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;       
    $max_rolls = -INF;
    
    // Q8 - for loop to process rolls
    for ($count = 0; $count <10000; $count++) {
        $rolls = 1;
        while (mt_rand(1, 6) !=$number_to_roll) {
            $rolls++;
        }
        $total += $rolls;            
        $max_rolls = max($rolls, $max_rolls);
    }
    $average_rolls = $total / $count;

    break;

}
include ‘loop_tester.php’;
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service