I would like iterate on each Object in my session variable. 'items' is of type item which is a class I've created to store book information. I have done a count($_SESSION['items']) which returns a 1 so i thought it should iterate at least once. But no luck. Thanks
foreach ($_SESSION['items'] as $item) {
echo TEST;
$sql = 'SELE开发者_运维问答CT * FROM item WHERE Item_ID = '.$item->Get_Item_ID;
var_dump($sql);
}
Why are you storing an object in a session variable?
http://php.net/manual/en/function.serialize.php
Should be able to serialize and deserialize the object into a format that you can store within a cookie.
I think $_SESSION can only store strings and arrays.
I would like to note that you should never perform a query in a nested loop like that. Rather, you should qualify your query like such....
$sql = "SELECT * FROM item WHERE Item_ID IN (".implode(",", $_SESSION['items']).")";
Of course, you need to ensure that $_SESSION['items']
is an array with at least one value, and that all values in $_SESSION['items']
are in fact integers (or can pass for integers [such as string "6"
]).
I think you forgot to serialize/unserialize the object. It's not possible to just assign a object into a session variable. This is because the session gets written out into a file where your object stops to exist. Use serialization to store an object into the Session.
Page 1:
$user = new User();
$_SESSION['user'] = serialize($user);
Page 2:
$user = unserialize($_SESSION['user']);
Best wishes,
Fabian
One final thing, ensure that the class definition for type 'item' is loaded before de-serializing your objects from the session. Otherwise you'll get errors when trying to access fields or methods on the object.
精彩评论