sk00ter;10988824 wrote:I'd like to be able to add an unlimited amount of inputs, rather than restricting it to 3 or 4.
In that case, you might consider two modifications:
A change to findAvg() similar to what I mentioned above, where the function is able to handle an arbitrary number of arguments.
A change to your form... create multiple text boxes all with the name "numbers[]" (or whatever - the key is the '[]' at the end of the name).
That way, $_POST['numbers'] will actually become an array itself... e.g. one that you could simply pass directly to your (new and improved) findAvg() function. EDIT: Woops, wasn't thinking... you can't just pass it directly (since my function above still expects each input to be an argument in the function call); however, it's still a one-liner thanks to the function [man]call_user_func_array/man.
You could even use a touch of Javascript and include an "Add New Input" button that allows the user to create as many textboxes as they want.
sk00ter;10988824 wrote:I agree, it simply doesn't work. Found that out through trial/error.
But it never should have even come to trial and error. [man]explode/man takes a single string and 'explodes' it into a bunch of pieces (array elements, in other words). $_POST is already an array containing all variables/entities that were POST'ed.
sk00ter;10988824 wrote:A) Apart from making several POST forms, is there any way to separate input data in a single field?
😎 Can I somehow manipulate a $_POST value to be read as multiple inputs of integers rather than a single array so it can be understood by the findAvg() function?
Yes to both - [man]explode/man is one possible answer (as Derokorian further points out above).
sk00ter;10988824 wrote:From what you're saying, it sounds like it is impossible for $_POSTs to carry multiples of input data like a series of numbers: 34,54,65,78
Of course it's possible... but that data isn't a "series of numbers" but rather just a plain old character string. And yes, POST'ed data is just a bunch of strings identified by some key or name (e.g. the form element's name).
sk00ter;10988824 wrote:PHP just recognizes this data as the whole, boolean phrase rather than a series of variables.
Er... no, PHP doesn't "recognize" anything. Garbage in, garbage out - PHP just sees a bunch of string data that was POST'ed to it, so that's all it stores in $_POST['avg'] - just a plain old string.
And besides, how is "34,54,65,78" a "boolean phrase" in any way?
sk00ter;10988824 wrote:Thus, the output is always "1".
Nope... at least, not for me. What you're doing here:
findAvg($_POST['avg']);
is the same as:
findAvg('34,54,65,78');
which is going to produce output like:
Warning: Missing argument 2 for findAvg()
Warning: Missing argument 3 for findAvg()
Warning: Missing argument 4 for findAvg()
Notice: Undefined variable: val2
Notice: Undefined variable: val3
Notice: Undefined variable: val4
8.5
with the '8.5' being the expected result (34/4 = 8.5)... as well as the PHP error messages (since you aren't calling the function with the proper number of arguments).
sk00ter;10988824 wrote:While you did re-write my code, it didn't really change the functionality.
I suppose nothing changed the functionality... other than the fact that my version is functional and yours is nonfunctional for any number of inputs other than 4.... but yeah, other than that... 😉
sk00ter;10988824 wrote:Your code and my code are still limited by the $POST value, at least, by a single one.
Actually no, neither of our functions are "limited" in any way by any $POST value. If you don't believe me, take a look at my version again:
function findAvg()
{
return array_sum(func_get_args()) / func_num_args();
}
and point out where $_POST appears in the code and/or has any direct correlation.
sk00ter;10988824 wrote:Using multiple $_POSTs will work, but that's not what I'm trying to do. Once again I'm attempting to write a code that lets a user put in an unlimited amount of inputs(variables, arguments) for the findAvg() function through a POST element within a HTML page.
Well for one, if that truly is your goal then you're going to need to change the findAvg() function as alraeady stated above. Otherwise, it will produce incorrect results for any number of inputs other than 4.
Second, it's not that you need "multiple $_POSTs", just multiple somethings. Whether you use multiple form entities with different names, multiple entities with a shared name like 'numbers[]', or a single form entity whose string data gets processed into multiple pieces (again, such as using [man]explode/man)... etc.
Derokorian;10988826 wrote:Basic example as follows:
//Given input from single form input
$input = '12,27,15,85,33,71,49';
// put the numbers into an array
$nums = explode(',',$input);
// initialize the total variable
$total = 0;
// Add each number to the total
foreach($nums as $num) {
$total += $num;
}
// Echo the average
echo $total/sizeof($nums);
Aye, basic 'nuff, although I'd personally stick with the use of array_sum() for its conciseness:
//Given input from single form input
$input = '12,27,15,85,33,71,49';
// put the numbers into an array
$nums = explode(',',$input);
// Echo the average
echo array_sum($nums)/sizeof($nums);