i want send array through session. here code:
$result = mysqli_query($conn, trim($query1)); if(!$result) { die('invalid query: '.mysqli_error($conn)); } while ($data = mysqli_fetch_row($result)) { echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td><td>$data[4]</td><td>$data[5]</td><td>$data[6]</td><td>$data[7]</td><td>$data[8]</td></tr>"); $temp[] = $data[0]; } $_session['data'] = $temp;
but have fails. , in php file, want display these values.
foreach ($_session['data'] $key) { # code... }
how can save variable $_session?
probably might .
function sessionstore($sessionkey,$value) { $sessionvalue = null; if (is_array($value)) { $sessionvalue = json_encode($value); } else{ $sessionvalue = $value; } $_session[$sessionkey] = $sessionvalue; } function sessionretrieveall($sessionkey){ $value = $_session[$sessionkey]; $result = json_decode($value,true); if (json_last_error() === json_error_none) { return $result; } return $value; } function sessionretrieve($sessionkey,$arraykey = null){ $arr = $this->sessionretrieveall($sessionkey); if(!is_array($arr)){ return $arr; } else if($arraykey != null){ return isset($arr[$arraykey]) ? $arr[$arraykey] : null; } return $arr; }
and can implement
$this->sessionstore('test',['a' => 'b', 'c' => 'd']); print_r($this->sessionretrieve('test')); //returns array echo $this->sessionretrieve('test','a'); // returns 'b' $this->sessionstore('test2','just string'); echo $this->sessionretrieve('test2'); // returns 'just string'
cheers !!!
Comments
Post a Comment