PHP在数组中使用会话变量

时间:2010-05-02 14:48:11

标签: php

我的问题是如何将这些会话变量放入数组中?我尝试了无数种方法,但没有一种方法可行 不确定要放入阵列中的内容以及没有和如何添加它们的内容 目前,当我填写表格时,数据会显示在表格中。

接下来,当我按下将我带回同一表格的超链接时,我希望再次输入数据 应将此数据添加到同一显示表中的新行中。

最诚挚的问候。

下面的代码(原谅我不是英文)。

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

第二档:

<?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 个答案:

答案 0 :(得分:4)

1 extract()

2

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

$$key将为您创建$standnaam var等等。

答案 1 :(得分:2)

将数组放在 $ _SESSION中会更容易吗?

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

然后,如果你想在其他地方访问数组,你可以(例如):

$v = $_SESSION['my_array'];

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

使用这样的数组的好处是可以更容易地使用foreach迭代它们来验证/处理它们的数据,并且您可以更轻松地划分$ _SESSION中的各种内容(而不是,例如,在$中运行所有内容) _SESSION通过validate()可能不合适。)

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>';