0

My question is how do i put these session variables into a array? I have tried countless ways but none of them work.
Not really sure what to put in a array and what no and how to adress them.
Currently when i fill in the form the data gets displayed in a table.

Next when i press the hyperlink that takes me back to the same form, i wish to enter data again.
This data should be added in a new row in the same display table.

Best Regards.

The code below (pardon me that it is not english).

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0            Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>ExpoFormulier</title>

<body>

<?php
if (!empty($_POST))
{
$standnaam = $_POST["standnaam"];
$oppervlakte = $_POST["oppervlakte"];
//value in the form van checkboxes op 1 zetten!
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;  
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;  
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 


if (is_numeric($oppervlakte)) 
{
    $_SESSION["standnaam"]=$standnaam;
    $_SESSION["oppervlakte"]=$oppervlakte;
    $_SESSION["verdieping"]=$verdieping;
    $_SESSION["telefoon"]=$telefoon;
    $_SESSION["netwerk"]=$netwerk;
    header("Location:ExpoOverzicht.php"); 
}
else 
{
    echo "<h1>Foute gegevens, Opnieuw invullen a.u.b</h1>";
}  
}

?>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
    <tr>
        <td>Standnaam:</td>
        <td><input type="text" name="standnaam" size="18"/></td>
    </tr>
    <tr>
        <td>Oppervlakte (in m^2):</td>
        <td><input type="text" name="oppervlakte" size="6"/></td>
    </tr>
    <tr>
        <td>Verdieping:</td>
        <td><input type="checkbox" name="verdieping" value="1"/></td>
    </tr>
    <tr>
        <td>Telefoon:</td>
        <td><input type="checkbox" name="telefoon" value="1"/></td>
    </tr>
    <tr>
        <td>Netwerk:</td>
        <td><input type="checkbox" name="netwerk" value="1"/></td>
    </tr>
    <tr>
        <td><input type="submit" name="verzenden" value="Verzenden"/></td>
    </tr>
</table>

</form>

Second File:

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>ExpoOverzicht</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link href="StyleSheetExpo.css" rel="stylesheet" type="text/css" />  
</head>

<body>

<h1>Overzicht van de ingegeven standen in deze sessie</h1>

<?php

$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];

$result1 = 0; 
$result2 = 0;
$result3 = 0;
$prijsCom = 0;
$prijsVerdieping = 0;

for ($i=1; $i <= $oppervlakte; $i++)
{   
    if($i <= 10)
    {
       $tarief1 = 1 * 100;
       $result1 += $tarief1;
    }

    if($i > 10 && $i <= 30)
    {
        $tarief2 = 1 * 90;
        $result2 += $tarief2;      
    }

    if($i > 30)
    {
        $tarief3 = 1 * 80;
        $result3 += $tarief3;
    }  
}
$prijsOpp = $result1 + $result2 + $result3;


if($verdieping == 1)
{
    $prijsVerdieping = $oppervlakte * 120;
}


if(($telefoon == 1) || ($netwerk == 1))  
{
    $prijsCom = 20;
}

if(($telefoon == 1) && ($netwerk == 1))
{
    $prijsCom = 30;
}


$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; 

echo "<table class=\"tableExpo\">";

    echo "<th>Standnaam</th>";
    echo "<th>Oppervlakte</th>";
    echo "<th>Verdieping</th>";
    echo "<th>Telefoon</th>";
    echo "<th>Netwerk</th>";
    echo "<th>Totale prijs</th>";

        echo "<tr>";     
            echo "<td>".$standnaam."</td>";
            echo "<td>".$oppervlakte."</td>";
            echo "<td>".$verdieping."</td>";
            echo "<td>".$telefoon."</td>";
            echo "<td>".$netwerk."</td>";
            echo "<td>".$totalePrijs."</td>";
        echo "</tr>";

echo "</table>";

?>

<a href="ExpoFormulier.php">Terug naar het formulier</a>

</body>
</html>

</body>
</html>

2 Answers 2

4

1 extract()

2

$keys = array('standnaam', 'oppervlakte', ...);
foreach ( $keys as $key ) {
  $$key = isset($_SESSION[$key]) ? $_SESSION[$key] : 0;
}

$$key will create for you a $standnaam var and so on..

Sign up to request clarification or add additional context in comments.

Comments

2

Might it be easier to put your array in $_SESSION?

$_SESSION['my_array'] = array('standnaam' => 'foo', 'oppervlakte' => 'bar');

Then if you want to access the array somewhere else, you could (for instance):

$v = $_SESSION['my_array'];

validate($_SESSION['my_array']['standnaam']);

The benefit of using arrays like this is that it's easier to iterate over them with foreach to validate / process their data, and you can more easily compartmentalize the various things in $_SESSION (rather than, for instance, running everything in $_SESSION through validate() as may not be appropriate).

E.g.:

$_SESSION['form_data'] = array('Standnaam' => 'foo', 'Oppervlakte', => 'bar');

echo '<table><tr>';
foreach($_SESSION['form_data'] as $var)
{
  echo "<td>$var</td>";
}
echo '</tr></table>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.