So I am dealing with customer shopping basket. The best way I found suitable for my purpose was to store objects (with setting properties for each item attribute) in an array. So I ended up with an array of objects ItemObject type.
I read the best way to store this array of objects in MySQL table, was to serialize it hence converting it to string and then storing it. Then when I need it I can unserialize() it with the php function.
I did just that...and now that I unserialize it, I get an error....the code follows for the ItemObject, and the unserialization part. How can I unserialize that string back to array of objects of type ItemObjects so that I could iterate and print out the details of each item?
<?php
$dateSelected = $_GET['date'];
echo "Date: ".$dateSelected . "<br>";
mysql_connect();
mysql_select_db("dashjkd");
$result = mysql_query("SELECT order_object FROM orders WHERE orderDate = '".$dateSelected."'");
if(mysql_num_rows($result) > 0){
$temp;
while($row = mysql_fetch_assoc($result)){
$temp = $row['order_object'];
//print_r($temp);
}
echo "Array: <br>";
$basketObjArray = array();
$basketObjArray = unserialize($temp);
print_r($basketObjArray);
foreach($basketObjArray as $key => $value)
{
echo "Key: " . $key;
echo "Value: ". $value;
}
}else{
echo "Something went horribly wrong..try again!";
}
?>
And this unserilization code leads to this error and strange output:
Date: 2012-04-18
Array:
Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ItemObject [itemNameVar] => whitethoab [itemQtyVar] => 12 [itemPriceVar] => 10 ) [1] => stdClass Object ( [itemNameVar] => woolthoab [itemPriceVar] => 10 [itemQtyVar] => 2 ) [2] => stdClass Object ( [itemNameVar] => shemag [itemPriceVar] => 4 [itemQtyVar] => 1 ) [3] => stdClass Object ( [itemNameVar] => jacket [itemPriceVar] => 12 [itemQtyVar] => 2 ) ) Key: 0
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in /home/sdsds/public_html/laundry/customerorder.php on line 21
the itemObject I designed was very simple and worked before when I attempted to display the array before serializing it:
class ItemObject{
public $itemNameVar;
public $itemQtyVar;
public $itemPriceVar;
}
Thanks for the help :)