JustinK101 Posted February 3, 2009 Share Posted February 3, 2009 How and what is the best way to keep appending objects to an array that is stored in session? For example: $images = array(); array_push($images, new Image('myimage.png', 350, 434, 45.1); $_SESSION['images'] = $images; Then after a page refresh simply keep appending new items into the $_SESSION['images'] array. So I was thinking something like: { AFTER A PAGE REFRESH } // Get the current items/objects in $_SESSION['images'] store them a temporary array, add the new item/object to the temporary array, and set the session $_SESSION['images'] equal to the NEW temporary array. I am not sure if there is a more elegant solution though. Thinking something like: $temp_images = array(); for($i = 0; $i < count($_SESSION['images']); $i++) { array_push($temp_image, $_SESSION['images'][i]); } array_push($temp_images, new Image('yetanotherimage.png', 343, 232, 454.3); $_SESSION['images'] = $temp_images; Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/ Share on other sites More sharing options...
uniflare Posted February 3, 2009 Share Posted February 3, 2009 If you just want to "Add an Item" to the "End" of an array; use the declaration operator: [] eg; $_SESSION['images'][] = new Image('myimage.png', 350, 434, 45.1); Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/#findComment-753374 Share on other sites More sharing options...
JustinK101 Posted February 3, 2009 Author Share Posted February 3, 2009 uniflare, Close, but getting weird __PHP__Incomplete_Class_Object objects appended to the start of the session array, seems to be one of these incomplete class objects every time I refresh and add a new image object to the array. Array ( [images] => Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => Image [filename] => brianwalking.gif [width] => 96 [height] => 96 [size] => 48 ) [1] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => Image [filename] => feedme.jpg [width] => 100 [height] => 100 [size] => 4.9 ) [2] => Image Object ( [filename] => hulkpeter.gif [width] => 100 [height] => 100 [size] => 21.2 ) ) ) Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/#findComment-753377 Share on other sites More sharing options...
JustinK101 Posted February 3, 2009 Author Share Posted February 3, 2009 So serializing the object fixed my problem, but now the data looks retarded when I do print_r($_SESSION), is that storing the object right in the session? My Serialization Fix $new_image = new Image($my_uploader->file['name'], $my_uploader->file['width'], $my_uploader->file['height'], round(($my_uploader->file['size'] / 1000), 1)); $_SESSION['images'][] = serialize($new_image); Output Of print_r($_SESSION); Array ( [images] => Array ( [0] => O:5:"Image":4:{s:8:"filename";s:16:"brianwalking.gif";s:5:"width";i:96;s:6:"height";i:96;s:4:"size":48;} [1] => O:5:"Image":4:{s:8:"filename";s:10:"feedme.jpg";s:5:"width";i:100;s:6:"height";i:100;s:4:"size":4.9000000000000003552713678800500929355621337890625;} [2] => O:5:"Image":4:{s:8:"filename";s:13:"hulkpeter.gif";s:5:"width";i:100;s:6:"height";i:100;s:4:"size":21.199999999999999289457264239899814128875732421875;} ) ) Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/#findComment-753380 Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 You just need to unserialize the data again before you can use it. However, objects placed into the $_SESSION array should automatically be serialized. Are you sure your class definition for the Image object is within scope for all pages accessing the $_SESSION? Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/#findComment-753382 Share on other sites More sharing options...
JustinK101 Posted February 3, 2009 Author Share Posted February 3, 2009 thorpe, Thanks for the reply. Not sure exactly what is meant by are you sure the class definition for the Imaeg object is within scope, but the serialize() and unserialize() trick appears to have solved the problem, so I assume scope may have not been the issue. Link to comment https://forums.phpfreaks.com/topic/143582-appending-object-items-into-a-session-variable/#findComment-753386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.