sgalatas Posted November 10, 2007 Share Posted November 10, 2007 Hello, My name is Sherrie and I am not a php newbie. I am trying to create an array from screen input. I need the array to gather football team scores and display the mean, mode, and median of the array. Here is my code so far. Can anyone tell me if I'm on the right track? ??? $score = ""; $len = 0; $x = 0; $ct = 0; $scoreArray1 = array(); $scoreArray2 = array(); $score1 = $_GET['SCORE1']; $score2 = $_GET['SCORE2']; echo "<p>Scores: $score1 $score2 </p>"; if ($score != null) if (is_numeric($score)== false) echo "Please enter correct value"; $len = count($scoreArray); for ($x=0; $x < $len; $x++) { echo "SCORES [$x]<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 10, 2007 Share Posted November 10, 2007 Well, I have no idea if this script accomplishes anything...? $scoreArray1 and 2 are defined as arrays, but are never populated with anything... they're empty. $score1 and 2 are populated by GET. So let's say http://www.someurl.com/script_name.php?SCORE1=32&SCORE2=16 Now we have: $score1 = '32' //string val 32 $score2 = '16' //string val 16 You echo that... all good. IF $score is NOT NULL. Hmm... well, $score could never be NULL because it's never in jeopardy of being populated with NULL anywhere. So that will always equal TRUE and the next part will execute. IF $score is NUMERIC. Again, $score has never existed until now, so not only can it not be NULL, it can't be NUMERIC. It doesn't even exist. You compare it with FALSE, it will always be FALSE, so the next part executes, which will echo a prompt to enter a correct value. I guess so! hehe Are you confusing $score with $score1 or $score2? Because they all 3 are separate variables... if the 'correct values' are input with GET['SCORE1'] and GET['SCORE2'], $score will never be numeric, and we are in a hopeless loop of being prompted to enter a correct value! At least there is no exit or die statement, so then we loop through an array SCORES, which isn't a legal variable without a $ in front... but before we discuss that, let's discuss the loop counter, which depends on the count of $scoreArray.. which doesn't even exist! Since the count of a non-existent array will be 0, your loop is saying: FOR ( $x = 0 ; WHILE 0 IS LESS THAN 0 ; Inc $x ) next pass FOR ( $x = 1 ; WHILE 1 IS LESS THAN 0 ; Inc $x ) This is going the wrong way obviously... because $len is defined by an empty array. Since the loop would never be satisfied, I'm going to predict a runaway script here that is eventually going to bring the server to its knees! Back to SCORES, since there's no $ in front, it's going to print to screen: SCORES [0] SCORES [1] SCORES [2] and up until the server cries Uncle. I'd say this script has a few issues! heh Let's try this: <?php // test for numeric data in score1 and score2 // (let's use lower-case variables when possible!) if ( is_numeric($_GET['score1']) && is_numeric($_GET['score2']) ) { // this is true, so let's assign easier variables (may need them later..?) $score1 = $_GET['score1']; $score2 = $_GET['score2']; // let's pack them into an array $scoreArray = array($score1, $score2 ); // let's forget the FOR loop and use FOREACH instead, which was designed // for looping over arrays! foreach ( $scoreArray as $key => $value ) { // we add 1 to $key because the first key is 0, not 1 echo "Score ". ($key + 1) . " = $value<br />"; } // I think we're done..! Oh, wait... } else { echo "Please enter correct value!"; // this prints if they weren't numeric } ?> So let's test it... http://www.your_url.com/your_script.php?score1=32&score2=16 Prints: Score 1 = 32 Score 2 = 16 http://www.your_url.com/your_script.php?score1=32 Prints: Please enter correct value! //--> no score2 submitted http://www.your_url.com/your_script.php?score1=foo&score2=bar Prints: Please enter correct value! //--> non-numeric data for both score1 and 2 PhREEEk Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2007 Share Posted November 10, 2007 It's not so bad with only two score values, but you don't want $score1 = $_GET['SCORE1']; $score2 = $_GET['SCORE2']; . . . $score20 = $_GET['SCORE20']; To avoid this, pass the form fields as an array by naming all the score fields on the form "SCORE[]" eg <form> <?php for ($i=1; $i <= 20; $i++) { echo "Score $i <input type='text' name='SCORE[]' size='5' /><br />"; } ?> <input type='submit' name='sub' value='Submit'> </form> To process <?php if (isset($_GET['SCORE'])) { $scores = array(); foreach ($_GET['SCORE'] as $val) { if (!empty($val)) { $scores[] = $val; echo $val, '<br/>'; } } $mean = array_sum($scores) / count($scores); echo "Mean : $mean"; } ?> Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 12, 2007 Author Share Posted November 12, 2007 Thanks you sooooo much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.