Page 1 of 2

Need help with Arrays

Posted: Sun Oct 23, 2011 11:07 am
by jetlife76
Ok i am trying to create a simple program that will allow mwe to use an array with 8 numbers, the output form should show the numbers in their original order that the user inputs them, in numerical order, Highest number, lowest number and then the average number. I am confused on what logic i would use to do this. If anyone could help me get started it would be much appreciated. here's waht i have so far.

Code: Select all

<?php
$Num1 = $_POST['fielda'];
$Num2 = $_POST['fieldb'];
$Num3= $_POST['fieldc'];
$Num4 = $_POST['fieldd'];
$Num5 = $_POST['fielde'];
$Num6 = $_POST['fieldf'];
$Num7 = $_POST['fieldg'];
$Num8 = $_POST['fieldh'];
$Numbers = array(1,2,3,4,5,6,7,8);
$big = max($Numbers);
$small = min($Numbers);
$average = array_sum($Numbers) / 8; 
?>

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 11:30 am
by Celauran
What are $Num1 through $Num8? They are defined but never used. Is $Numbers meant to always contain the values 1 through 8?

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 11:32 am
by jetlife76
$Num1 - $Num8 are the text boxes from the input form, yes those are the numbers in the ARRAY

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 11:42 am
by Celauran
Replace name="fielda" through name="fieldh" with name="numbers[]".

Code: Select all

$Numbers = $_POST['numbers'];
$big = max($Numbers);
$small = min($Numbers);
$average = array_sum($Numbers) / count($Numbers); 

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 11:53 am
by jetlife76
Ok u just confused me even more, where is the array in your code?

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:11 pm
by Celauran
Already specified in the form. That's what changing the name to numbers[] does. Notice the []?

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:20 pm
by jetlife76
'numbers[]' gives me an undefined index warning

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:36 pm
by Celauran
Can you post the code that's giving the warning?

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:40 pm
by jetlife76

Code: Select all

<?php
$Num1 = $_POST['numbers[]'];
$Num2 = $_POST['numbers[]'];
$Num3= $_POST['numbers[]'];
$Num4 = $_POST['numbers[]'];
$Num5 = $_POST['numbers[]'];
$Num6 = $_POST['numbers[]'];
$Num7 = $_POST['numbers[]'];
$Num8 = $_POST['numbers[]'];
//$nums = array[''];
$Numbers = $_Post['numbers'];
$big = max($Numbers);
$small = min($Numbers);
$average = array_sum($Numbers) / count($Numbers);
?>

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:44 pm
by Celauran
You've added a bunch of junk that wasn't in the code I posted. Why?

Code: Select all

<?php
$Numbers = $_POST['numbers'];
$big = max($Numbers);
$small = min($Numbers);
$average = array_sum($Numbers) / count($Numbers);
?>
Where is the form?

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 12:56 pm
by jetlife76
ok the code you suggested gave me a page full of errors, i dont understand what you are doing. can you explain it, because im not understanding where the array is in your code or what the code is doing

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 1:02 pm
by Celauran
I can't help you with the errors because a.) you haven't said what the errors are, and 2.) you still haven't posted your code beyond that little snippet. I'll explain mine, though, since you asked.

Code: Select all

$Numbers = $_POST['numbers'];
This assigns the value of $_POST['numbers'], which comes from the field(s) named "numbers[]" in your form, to the variable $Numbers. That's it. The rest of the code I posted is yours.

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 1:08 pm
by jetlife76
Notice: Undefined index: numbers in C:\wamp\www\Fall2011etoyo\Pro9Output.php on line 7
Warning: array_sum() expects parameter 1 to be array, null given in C:\wamp\www\Fall2011etoyo\Pro9Output.php on line 10
Warning: Division by zero in C:\wamp\www\Fall2011etoyo\Pro9Output.php on line 10
Warning: max() [function.max]: When only one parameter is given, it must be an array in C:\wamp\www\Fall2011etoyo\Pro9Output.php on line 8
Warning: min() [function.min]: When only one parameter is given, it must be an array in C:\wamp\www\Fall2011etoyo\Pro9Output.php on line 9
This is why i asked where is the array in your code?
I thought i had to have an array set up before i can do max -min/ array_sum ...

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 1:11 pm
by Celauran
Have you adjusted your form as well? $_POST['numbers'] is an array.

Re: Need help with Arrays

Posted: Sun Oct 23, 2011 1:19 pm
by jetlife76
here's the code for the form

Code: Select all

<html>
<body>

<h1 style="text-align:center;font-family:comic sans;color:brown;">Number<br></h1>
<h2 style="text-align:center;font-family:arial;color:green;">Cruncher<br></h2>
<h3 style="text-align:center;font-family:arial;color:blue;"><br>
Enter your 8 numbers
<form name="input" action="Pro9Output.php" method="post">
Number 1: 
<input type="text" name="numbers" size="2">
<br>
Number 2: 
<input type="text" name="numbers" size="2">
<br>
Number 3: 
<input type="text" name="numbers" size="2">
<br>
Number 4: 
<input type="text" name="numbers" size="2">
<br>
Number 5: 
<input type="text" name="numbers" size="2">
<br>
Number 6: 
<input type="text" name="numbers" size="2">
<br>
Number 7: 
<input type="text" name="numbers" size="2">
<br>
Number 8: 
<input type="text" name="numbers" size="2">
<br>

<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form> 
<p>
    Click "Submit" to arrange Numbers .
<br>Click "Reset" to clear form.
</p>
</h3>
</body>
</html>