【问题标题】:Delete index session in PHP在 PHP 中删除索引会话
【发布时间】:2012-08-26 00:31:09
【问题描述】:

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

谢谢

【问题讨论】:

  • 你试过使用函数session_unset()吗?您可以检查当前会话的索引并在必要时取消设置。
  • 如果您参考上面的数据,unset($_SESSION['chart'][1]) 将不起作用。 unset($_SESSION['chart'][0]) 应该删除正确的索引吗?
  • 请参考具体答案,打印数据有错误
  • 错误:注意:未定义的偏移量:0
  • 第二行末尾缺少分号:$_SESSION['chart'][0]['index'] = 0

标签: php session


【解决方案1】:

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

  • 不要在你的函数中回显,而是return(然后回显函数返回的内容)。这让您可以更灵活地处理结果。
  • 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

【讨论】:

  • 等一下,删除会话有问题,能否给我看一下我想删除的当前索引中的删除会话的代码,谢谢
  • 您的代码确实出现了很多错误,您检查和调试了吗?
【解决方案2】:

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

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

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

【讨论】:

  • 很多人一直在提供这个,但不幸的是,我的问题没有直接的答案,可能会用你以前尝试过的正确答案来回答这个问题,如果有任何直接的答案,将不胜感激而不是发布我之前已经声明过的东西。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2013-02-06
  • 2010-10-27
  • 2020-01-28
相关资源
最近更新 更多