Need help with Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Need help with Arrays

Post 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; 
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post by Celauran »

What are $Num1 through $Num8? They are defined but never used. Is $Numbers meant to always contain the values 1 through 8?
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post by jetlife76 »

$Num1 - $Num8 are the text boxes from the input form, yes those are the numbers in the ARRAY
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post 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); 
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post by jetlife76 »

Ok u just confused me even more, where is the array in your code?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post by Celauran »

Already specified in the form. That's what changing the name to numbers[] does. Notice the []?
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post by jetlife76 »

'numbers[]' gives me an undefined index warning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post by Celauran »

Can you post the code that's giving the warning?
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post 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);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post 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?
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post 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.
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post 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 ...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with Arrays

Post by Celauran »

Have you adjusted your form as well? $_POST['numbers'] is an array.
jetlife76
Forum Newbie
Posts: 24
Joined: Tue Oct 18, 2011 1:21 pm

Re: Need help with Arrays

Post 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>
Post Reply