For an assignment from my book, I need to make a PHP document that can calculate the average of a series (x) inserted variables using a SESSION of the type Array...
This is my first question on Stackoverflow and it's probably a dumb question. I already tried to look up some answers, and it's probably very easy but I don't know how to save an input in a session variable and then calculate the average of it.
This is what I have so far:
<!DOCTYPE html>
<html>
<head>
<title>Gemiddelde</title>
</head>
<body>
<strong>Test Form</strong>
<form action="" method="get">
<input type="text" name="cijfer">
<input type="submit" name="add" value="add">
</form>
<?php
session_start();
if (isset($_GET['add'])) {
$cijfer = array('ingevoerd' => $_SESSION['cijfer'] = $_GET['cijfer']);
}
?>
<?php
echo $_SESSION['cijfer'].'<br>';
?>
</body>
</html>
You can find an example here:
You only implicitly assign the latest input ($_GET['cijfer']
) to $_SESSION['cijfer']
, overwriting any previous value. That happens when you use the =
operation, of which I'm not sure you didn't do that intentionally.
The result of that assignment is also stored in $cijfer['ingevoerd']
, but that value is never used in your script.
So all your current script does, is take the currently inputted 'cijfer', store that in the session, and output it, forgetting everything it got before.
Here is a slightly improved and heavily commented version. It first puts the newly entered number in a variable and checks if the input is valid. If it is, casts it to an actual float, and appends it to the array 'ingevoerd'
, and then echoes the count.
I didn't want to give it all away, so I hope you can fill in the details on how to calculate the average from this array. :)
<?php
// This needs to be called first, because a cookie needs to be set,
// which needs to happen before any content is outputted.
session_start();
?><!DOCTYPE html>
... rest of your HTML goes here, left out for brevity ...
<?php
// Take the raw input into a variable. That's easier to work with.
$cijfer = $_GET['cijfer'];
// Check for numeric input. ctype_digit() will do if you want just integers.
// For floats, is_numeric(), or some regular expression might be a better check.
if (!ctype_digit($cijfer)) {
die('Only numbers, please');
}
// Add the new input. I cast to float here to already have numbers in the
// array, which makes calculating easier.
// The `[]` is a syntax for appending to an array. You may also use the
// more explicit array_push function.
$_SESSION['ingevoerd'][] = (float)$cijfer;
echo count($_SESSION['ingevoerd']) . ' numbers in the array';
// Calculate the average of all numbers in $_SESSION['ingevoerd'] and echo.
echo ....
Some general advice: Be explicit in your code, and try not to take any shortcuts. Assign stuff to variables first before proceeding to the next step. Simple variables are really cheap, and there is no reason not to use them, but it makes your code way easier to follow, and way easier to debug, because you can output the result of each individual step you take.