从count变量创建php $ _SESSION

时间:2017-04-04 15:48:45

标签: php

我正在尝试从示例代码计数($ result)创建$ _SESSION。我已经像这样对变量进行了decalred:

$result = array();
$result[] = $boxdest;

这在我运行它的页面上工作正常但我需要将值传递给在php mail()函数中使用的页面。我试过了

$_SESSION['result'] = $result;

但我得到的只是资源ID#40。如何将此变量传递给另一个页面。完整的代码示例:http://sandbox.onlinephpfunctions.com/code/ba0a34774e30c93edfdf02f531bb199c681021e3非常感谢

2 个答案:

答案 0 :(得分:1)

这更多的是对代码的批评,而不是对问题的解决方案:

$_SESSION['result'] = []; //Clear it in case there's an old value here.
if (isset($_POST['boxdest']) && is_array($_POST['boxdest']) && !empty($_POST['boxdest'])) { //Merged condition
    $destroydata = explode(',', $_POST['boxdest'][0]); //Split was deprecated
    $result = array_filter($destroydata);
    if (empty($result) { 
           $boxdesterror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'BOX DESTRUCTION ERROR! ' . '</span>' . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . 'You must enter a box for destruction' . '</span>';
           echo "<script language=\"JavaScript\">\n";
           echo 'alert("BOX DESTRUCTION ERROR:\nYou must enter a box for destruction.");';
           echo "</script>";
           echo $boxdesterror;
    } else {
        echo 'You wish to destroy ' . count($result) . ' box(es): ' . '<div style="word-wrap:break-word;white-space: pre-wrap;overflow:auto !important;height: 100px; width: 250px; border: 1px solid #666; background-color: #fff; padding: 4px;">' . '<span style="font-weight:bold;color: #000;">' . implode(', ', $boxdest) . '</span>' . '</div>' . '<p />';

        $_SESSION['result'] = $result; //Did you really need both?
        $flag = 1;
     }
}

这应该是相同的结果。我不知道为什么资源会从哪儿冒出来。

答案 1 :(得分:0)

U无法在PHP会话中存储任何Resource

https://stackoverflow.com/a/42389037/5361130

http://php.net/manual/en/function.serialize.php

修改

如果您的$resultmysqli_query之类的函数或类似结果,请使用mysqli_num_rows获取行数count

http://php.net/manual/en/mysqli-result.num-rows.php

$result = mysqli_num_rows($boxdest)

$_SESSION['result'] = mysqli_num_rows($boxdest)