0

我对 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])

谢谢

4

2 回答 2

0

假设有一个图表已经被内容填充然后我试图在这段代码中删除,我收到了未设置变量的错误unset($_SESSION['chart'][1])

从您的代码中可以看出,正确取消设置数据的最佳方法如下: DEMO

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

于 2012-08-31T09:04:51.470 回答
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
于 2012-08-31T08:51:20.310 回答