Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg1/exercises/ovning7/uppgift4/

index.php

index.php

51 lines UTF-8 Windows (CRLF)

<?php
    session_start
();

    if (!isset(
$_SESSION["tal"])) {
        
$_SESSION["tal"] = [];
    }

    if (isset(
$_POST["tal"]) && $_POST["tal"] !== "") {

        
$tal = $_POST["tal"];

        
$_SESSION["tal"][] = $tal;
    }

    if (
count($_SESSION["tal"]) > 0) {
        
$medel = array_sum($_SESSION["tal"]) / count($_SESSION["tal"]);
    }
?>

<html>
<body>

    <h1>Mata in tal</h1>

    <form method="post">
        <input type="number" step="any" name="tal" required>
        <button type="submit">Lägg till tal</button>
    </form>

    <h2>Alla inmatade tal:</h2>

    <?php
    
foreach ($_SESSION["tal"] as $tal) {
        echo 
$tal . "<br>";
    }
    
?>

    <h2>Medelvärde:</h2>

    <?php
    
if (count($_SESSION["tal"]) > 0) {
        echo 
$medel;
    } else {
        echo 
"Inga tal har matats in ännu.";
    }
    
?>

</body>
</html>