在PHP中删除索引会话

时间:2012-08-31 07:55:47

标签: php session

我有问题在PHP中引用会话。

我已经用PHP编写了用于设置会话的内容,我想删除它,但是我遇到了一些错误并且使用了UNSET(变量$ _SESSION)而感到困惑。也许任何人都可以帮我展示我应该怎么做这件事。提前致谢

                    $_SESSION['chart'] = array();
                    $_SESSION['chart'][0]['index']  = 0
                    $_SESSION['chart'][0]['type'] = $type;
                    $_SESSION['chart'][0]['idanimal'] = $iddog;
                    $_SESSION['chart'][0]['price'] = $price;
                    printdata(); // <== this is the function to print out the data

我想做的只是根据此会话中的索引进行删除

这里的功能是:

function printdata()
                {
                    $totalharga = 0;
                    if(is_array($_SESSION['chart']))
                    {
                    echo "<h3>"."Berikut adalah keranjang belanja anda" . "</h3>". "<br>";
                    $max = count($_SESSION['chart']);  
                    $th1 = "<th>" . "No" . "</th>";
                    $th2 = "<th>" . "Animal Type" . "</th>";
                    $th3 = "<th>" . "ID Binatang" . "</th>";
                    $th4 = "<th>" . "Harga" . "</th>";
                    $th5 = "<th>" . "Hapus Data" . "</th>";
                    echo "<table border=1>";
                    echo "<tr>";
                    echo $th1 ;
                    echo $th2;
                    echo $th3;
                    echo $th4;
                    echo $th5;
                    echo "</tr>";
                    for ($indexo = 0; $indexo < $max; $indexo++) 
                    {
                    echo "<tr>";
                    echo "<td>" . $indexo."</td>";
                    echo "<td>" . $_SESSION['chart'][$indexo]['type']."</td>";
                    echo "<td>" . $_SESSION['chart'][$indexo]['idanimal']."</td>";
                    echo "<td>" .  "Rp. " . $_SESSION['chart'][$indexo]['harga']. ",-"  ."</td>";
                    echo "<td>" . "<a href='deletesession.php?session=$indexo'>" ."Proses ".   "</a>"."</td>";
                    $totalharga += $_SESSION['chart'][$indexo]['harga'];
                    echo "</tr>";
                    }
                    echo "</table>" . "<br/>";
                    echo "<blockquote>" .  "Total Pembelian Item Yang Anda Pesan  =". "<h2>". "Rp." . $totalharga . ",-" ."</h2>" . "</blockquote>";
                    }else
                    {
                        echo "Chart is still Empty";
                    }
                }

我已经尝试过这个: 假设有一个已经由内容填充的图表然后我试图在此代码中删除,我收到了未设置变量的错误

unset($_SESSION['chart'][1])

谢谢

2 个答案:

答案 0 :(得分:0)

我发现了一些可以在代码中改进的内容:

  • 不要在函数中回显,返回(然后回显函数返回的内容)。这使您可以更灵活地处理对结果的处理。
  • PHP有一个用于迭代数组的内置循环, foreach 循环。你应该使用它而不是for。

所以这就是我所拥有的:

function print_session_data($session) {
    if (!is_array($session)) {
        return "Array is empty";
    }
    $table_data  = "";
    $total_harga = 0;
    foreach ($session as $index => $data) {
        $table_data .= <<<TABLE_DATA
        <tr>
            <td>$index</td>
            <td>{$data["type"]}</td>
            <td>{$data["idanimal"]}</td>
            <td>Rp. {$data["harga"]}</td>
            <td><a href="deletesession.php?session=$index">Proses</a></td>
        </tr>
TABLE_DATA;
        $total_harga += $data["harga"];

    }

    $result = <<<RESULT
<h3>Berikut abalah keranjang belanja anda</h3>
<table border="1">
    <thead>
        <tr>
            <th>No</th>
            <th>Animal Type</th>
            <th>ID Binatang</th>
            <th>Harga</th>
            <th>Hapus Data</th>
        </tr>
    </thead>
    <tbody>
        $table_data
    </tbody>
</table>
<blockquote>Total Pembelian Item Yang Anda Pesan = <strong>Rp. $total_harga</strong></blockquote>
RESULT;
    return $result;
}

$_SESSION['chart']                = array();
$_SESSION['chart'][0]['type']     = "Type";
$_SESSION['chart'][0]['idanimal'] = 6;
$_SESSION['chart'][0]['price']    = '$25';
$_SESSION['chart'][0]['harga']    = 25;
echo print_session_data($_SESSION["chart"]); // <== this is the function to print out the data

答案 1 :(得分:0)

  

假设有一张图表已经由内容填充,那么我   试图在此代码中删除,我收到了未设置的错误   变量unset($_SESSION['chart'][1])

从我的代码中可以看到,正确设置数据的最佳方法如下: DEMO

在再次使用会话变量索引之前,您必须检查$_SESSION['chart'][0]是否已设置。如果$_SESSION['chart']数组仍包含其他索引,则可以调用printdata();,否则表示会话为空。