strudel
strudel

Reputation: 61

PHP: How to use switch statement to retrieve radio button values

I'm working on a homework assignment while learning PHP for the first time. Everything is working, except for my switch statement. My professor is asking-

"Modify index.php

  1. Get the selected value from the "calculate" radio button you created
  2. Add a switch statement to set values for:
    1. only the average calculation if the user selected the "average" radio button
    2. only the total calculation if the user selected the "total" radio button
    3. both the average and total if the user selected the "both" button."

I'd like to provide my entire index.php file in case it's important to see everything-

<?php

//set default values to be used when page first loads
$scores = array();
$scores[0] = 70;
$scores[1] = 80;
$scores[2] = 90;

$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;

$score_total_f = '';
$score_average_f = '';

//take action based on variable in POST array
$action = filter_input(INPUT_POST, 'action');
switch ($action) {
case 'process_scores':
$scores = $_POST['scores'];

// validate the scores
$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 for scores.';
        $is_valid = false;
        break;
    }
}
if (!$is_valid) {
    break;
}

// process the scores
$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_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);


$calculate = filter_input(INPUT_POST, 'calculate');
switch($calculate) {
    case "average":
    $message_average = $score_average_f;
    break;
    case "total":
    $message_total = $score_total_f;
    break;
    case "both":
    $message_average = $score_average_f;
    $message_total = $score_total_f;
    break;
    default: die("Invalid type");
}

break;

case 'process_rolls':
    $number_to_roll = filter_input(INPUT_POST, 'number_to_roll', 
            FILTER_VALIDATE_INT);

    $total = 0;       
    $max_rolls = -INF;

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

Also, here is part of the other file where I had to create radio buttons-

<h3>What do you want to do?</h3>
<input type="radio" name="calculate" value="average" checked> Average<br>
<input type="radio" name="calculate" value="total"> Total<br>
<input type="radio" name="calculate" value="both"> Both<br>

<label>Scores:</label>
    <span><?php echo htmlspecialchars($scores_string); ?></span><br>

    <label>Score Total:</label>
    <span><?php echo $message_total; ?></span><br>

    <label>Average Score:</label>
    <span><?php echo $message_average; ?></span><br>
</form>

Thank you!

Again, everything is working fine when I test in XAMPP, just not the switch statement. I get no output of any kind.

Upvotes: 0

Views: 1009

Answers (2)

David
David

Reputation: 1

I'm doing the same exercise. You need to define your variables before all the code runs.

$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;
$message_average = 0;
$message_total = 0;

After I defined variables, $message_average and $message_total, everything worked fine.

Upvotes: 0

Andrew
Andrew

Reputation: 825

EDIT: Disregard my original answer, I tested out your original syntax and it seems to work fine. Sorry, that was a mistake on my part, though I'd still say the new code is more elegant.

There seems to be an issue with a misplaced break; - Here is a full working code for to 'process_scores' case:

case 'process_scores':
    $scores = $_POST['scores'];

    // validate the scores
    $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 for scores.';
            $is_valid = false;
            break;
        }
    }
    if (!$is_valid) {
        break;
    }

    // process the scores
    $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_f = number_format($score_total, 2);
    $score_average_f = number_format($score_average, 2);


    $calculate = filter_input(INPUT_POST, 'calculate');
    $score_average_f = number_format($score_average, 2);
    $score_total_f = number_format($score_total, 2);
    switch($calculate) {
        case "average":
        echo "Average: " . $score_average_f;
        break;
        case "total":
        echo "Total: " . $score_total_f;
        break;
        case "both":
        echo "Average: " . $score_average_f . "<br />";
        echo "Total: " . $score_total_f;
        break;
        default: die("Invalid type");
    }

    break;

I'm not sure about other the other part of your code, but I tested this and got the intended results. If you still see nothing, check what's in your $_POST variables. Also as a general advice for debugging: in a situation like this, just go through your code and echo stuff out inside and outside of every loop or function you believe your code should reach, to see where it gets derailed. It may not sound too professional, but it sure gets the job done.

Upvotes: 0

Related Questions