Trying to get rid of an error message

If the scores are empty or not numeric, display: “You must enter three valid scores”. The scores are being processed but the message above continues to be displayed. I can’t figure out how to make it stay when needed but go away when valid numbers are entered. Look at the comment “Validate the scores”.

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

    // validate the scores
    for($i=0; $i < count($scores); $i++) {
		if(empty($scores) || !is_numeric($scores)) {
			$scores_string = 'You must enter three valid numbers for scores.';
		}	
            
		break;

    // process the scores
    /************************************************
	* TODO: add code to calculate the score total   *
	************************************************/
      $scores_string = '';
      foreach ($scores as $s) {
        $scores_string .= $s . '|';
	}

}     $s = array();
      $s[0] = 70;
      $s[1] = 80;
      $s[2] = 90; 
      $score_total = array_sum($scores); // Total should be 240
      echo $score_total; // Should output 240
		
    $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;

    
	for($count = 0; $count<1000; $count++) {
		$rolls = 1;
		while (mt_rand(1, 6) != 6) {
		$rolls++;

}
$total += $rolls;
$max_rolls = max($rolls, $max_rolls);
}
$average_rolls = $total / $count;

    break;

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

do you have 3 input text in your form with different names or values

Hi Jay,

See if this works for you instead:
[php]for($i=0; $i < count($scores); $i++) {
if(!is_numeric($score[$i])) {
$scores_string = ‘You must enter three valid numbers for scores.’;
}[/php]

I removed the empty($scores) because $scores is an array and if it is empty, you will never get here in the first place. Also, you need to do your !is_numeric check against the array element instead of the actual array.

Changing this should at least get you on track, but it won’t specifically validate that there are three valid numbers. If you want to force exactly three numbers, you could add an if statement prior to the for verifying that count($scores) == 3.

To calculate the total score, once you have verified the array is all numeric, you can simply use something like this:[php]$total_score = array_sum($scores);[/php]

You are also going to need to get rid of the unconditional break after your if statement. This will prevent the for loop from executing properly. If I am understanding correctly what you are looking to do, something like this might work:[php]$error = $scores_string = ‘’;
if(count($scores)==3)
{
for($i=0; $i<3; $i++)
{
if(!is_numeric($scores[$i])) $error = ‘Not a number.’;
}
}
else $error = ‘You must enter three valid numbers.’;

if(empty($error))
{
$total = array_sum($scores);

$score_total = number_format($total, 2);
$score_average = number_format($total/3,2);
$scores_string = implode(' | ',$scores);    

}
else $scores_string = $error;[/php]

Obviously you will need to implement this (or parts of it) into your code, and I may be misunderstanding your intent. Let me know if you have any questions.

Sponsor our Newsletter | Privacy Policy | Terms of Service