将会话保存在数组中并计算平均值

时间:2019-02-11 11:30:35

标签: php session-variables

对于我的书中的作业,我需要制作一个PHP文档,该文档可以使用Array类型的SESSION计算一系列(x)插入变量的平均值。

这是我关于Stackoverflow的第一个问题,可能是一个愚蠢的问题。 我已经尝试查找一些答案,这可能非常简单,但是我不知道如何将输入保存在会话变量中,然后计算平均值。

这是我到目前为止所拥有的:

<!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>

您可以在此处找到示例:

enter image description here

2 个答案:

答案 0 :(得分:1)

您仅将最新的输入($_GET['cijfer'])隐式分配给$_SESSION['cijfer'],从而覆盖所有先前的值。当您使用=操作时会发生这种情况,但我不确定您是否有意这样做。

该分配的结果也存储在$cijfer['ingevoerd']中,但是该值从未在脚本中使用。 因此,您当前的脚本所做的只是获取当前输入的“ cijfer”,将其存储在会话中,然后输出它,而忘记了之前获得的所有内容。

这是一个稍作改进并受到强烈评论的版本。它首先将新输入的数字放入变量中,并检查输入是否有效。如果是,则将其强制转换为实际的float,然后将其附加到数组'ingevoerd',然后回显计数。

我不想放弃所有内容,因此希望您可以填写有关如何从该数组计算平均值的详细信息。 :)

<?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 ....

一些一般性建议:在您的代码中要明确,并尽量不要采用任何捷径。在继续下一步之前,首先将内容分配给变量。简单变量确实很便宜,没有理由不使用它们,但是它可以使您的代码更易于遵循和调试,因为您可以输出所采取的每个步骤的结果。

答案 1 :(得分:0)

感谢我得到@GolezTrol和@Martin的快速回复 我把任务尽量简单了。 该代码现在可以工作了:

编辑:现在我很高兴:)

<!DOCTYPE html>
<html>
<head>
    <title>Average</title>
</head>
<body>

<strong>Test Form</strong>
<form action="" method="get">
    <input type="number" name="cijfer">
    <input type="submit" name="add" value="add">
</form>

<?php 
    session_start();
    // unset($_SESSION['cijfer']);
    $cijfer = $_GET['cijfer'];
        if (isset($_GET['add'])) {
            $_SESSION['cijfer'][] = $_GET['cijfer'];
        }
?>

<?php
    $count = count($_SESSION['cijfer']);
    $addingUp = array_sum($_SESSION['cijfer']);
    $amount = count($_SESSION['cijfer']);
    $outcome = $addingUp / $count;

    echo '<pre>';
    print_r($_SESSION['cijfer']);
    echo '</pre>';
    echo "The number of entered digits is:  ".$count;

    echo "<br />Added together: ".$addingUp;
    echo "<br />The average of everything together is: ".round($outcome, 1);
    if ($count > 10) {
        unset($_SESSION['cijfer']);
    }
?>

</body>
</html>