douyao3895
douyao3895
2017-04-04 15:48

从count变量创建php $ _SESSION

已采纳

I am trying to create a $_SESSION from example code count($result). I have decalred the variable like thus:

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

This is working fine on the page where I run it but I need to pass the value to a page to use in php mail() function. I have tried

$_SESSION['result'] = $result;

But all I get is Resource id #40. How do I pass this variable to another page. Full code example here: http://sandbox.onlinephpfunctions.com/code/ba0a34774e30c93edfdf02f531bb199c681021e3 Many thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dotn30471 dotn30471 4年前

    This is more of a critique of the code rather than a solution to the problem:

    $_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\">
    ";
               echo 'alert("BOX DESTRUCTION ERROR:
    You 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;
         }
    }
    

    This should be the same results. I don't see why a resource would pop up from nowhere though.

    点赞 评论 复制链接分享
  • dongshenjie3055 dongshenjie3055 4年前

    U can't store any Resource in PHP session

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

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

    EDIT

    if your $result is result of function like mysqli_query or something like this, use mysqli_num_rows to get number of rows not count

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

    $result = mysqli_num_rows($boxdest)
    

    or

    $_SESSION['result'] = mysqli_num_rows($boxdest)
    
    点赞 评论 复制链接分享