删除PHP中的索引会话


Delete index session in PHP

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

感谢

我发现您的代码中有几个可以改进的地方:

  • 不要在函数中回显,返回(然后回显函数返回的内容)。这让你在处理结果时有了更多的灵活性
  • 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

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

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

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