0

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

user1532468
  • 1,723
  • 8
  • 41
  • 80

2 Answers2

1

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\">\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;
     }
}

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

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Thanks for that. What is the best way to count items in an array and create session from same. more like a size option – user1532468 Apr 04 '17 at 17:18
  • You could do `$_SESSION["resultSize"] = count($result)` and use the resultSize entry instead if you just want the size. – apokryfos Apr 04 '17 at 17:19
  • This is the output from var_dump which dosen't seem right does it?: 'result' => array (size=2) 0 => array (size=1) 0 => string 'DEMO10051B' (length=10) 1 => array (size=2) 0 => string 'DEMO10051B' (length=10) 1 => string 'DEMO10052A' (length=10) – user1532468 Apr 04 '17 at 17:30
  • That doesn't look right. I would have expected result to be an array of strings using this code. Are you sure there's nothing else going on in the 2nd page which may alter the result? – apokryfos Apr 04 '17 at 17:36
0

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)
Community
  • 1
  • 1
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31