Hello,
I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see page 3) the data displays just fine with the serialization added to it.
So basically I need help unserializing and displaying the data so I can display it in my shopping cart nice and clean.
Here is page 1(index2.php) which is a form that asks for opening name, width and height.
<?php
if (isset($_COOKIE['count'])) {
$count = $_COOKIE['count'] + 1;
} else {
$count = 1;
}
setcookie('coun t', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
echo "$count";
echo "<form action='add.php ?id=$count' method='post'>
<input name='name' type='hidden' value='$count'>
Opening name<br>
<input name='name' type='text' size='30'>
</b></p>
<p><b> Width<br>
<input name='w' type='text' size='30'>
</b></p>
<p><b> Height<br>
<input name='h' type='text' size='30'>
</b></p>
<p>
<input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'>
<br>
</p>
</form>";
?>
The second page(add.php) receives the data, creates a serialized array and adds it to the cookie just fine.
<?php
$count= $_GET['id'];
$name= $_POST['name'];
$w= $_POST['w'];
$h= $_POST['h'];
$serialized_dat a = serialize (array ($count, $name, $w, $h));
setcookie("myco okie[$count]", $serialized_dat a, time()+13600);
echo "Opening was Added<br><br>";
echo "$count";
echo "<br><br>";
echo "$name";
echo "$w";
echo "$h";
echo "<br><br>";
echo "<a href='index2.ph p'>Add another opening!</a><br>";
echo "<a href='show.php' >Show!</a><br>";
?>
Where the problem is when you click show (show.php) you see the serialized data.
I am trying to restore the data with an unserialize (see below line 8), but when I add that line of code, my data disappears instead of being stripped of the serialization. When I remove line 8, the data displays serialized.
So something is wrong, I guess with my unserialize method.
<?php
if($_COOKIE["mycookie"]) {
foreach( $_COOKIE[mycookie] as $key => $value)
{
$value2 = unserialize ($value);
echo "Number $key : Description $value2";
}
echo "<a href='index2.ph p'>Add Opening</a><br><br>";
}
else {
print "No Items in your cart<br><br>";
echo "<a href='index2.ph p'>Add Opening</a><br><br>";
}
?>
I have tried all weekend long and could use some help! Thanks
-Mr L8Knight
I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see page 3) the data displays just fine with the serialization added to it.
So basically I need help unserializing and displaying the data so I can display it in my shopping cart nice and clean.
Here is page 1(index2.php) which is a form that asks for opening name, width and height.
<?php
if (isset($_COOKIE['count'])) {
$count = $_COOKIE['count'] + 1;
} else {
$count = 1;
}
setcookie('coun t', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
echo "$count";
echo "<form action='add.php ?id=$count' method='post'>
<input name='name' type='hidden' value='$count'>
Opening name<br>
<input name='name' type='text' size='30'>
</b></p>
<p><b> Width<br>
<input name='w' type='text' size='30'>
</b></p>
<p><b> Height<br>
<input name='h' type='text' size='30'>
</b></p>
<p>
<input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'>
<br>
</p>
</form>";
?>
The second page(add.php) receives the data, creates a serialized array and adds it to the cookie just fine.
<?php
$count= $_GET['id'];
$name= $_POST['name'];
$w= $_POST['w'];
$h= $_POST['h'];
$serialized_dat a = serialize (array ($count, $name, $w, $h));
setcookie("myco okie[$count]", $serialized_dat a, time()+13600);
echo "Opening was Added<br><br>";
echo "$count";
echo "<br><br>";
echo "$name";
echo "$w";
echo "$h";
echo "<br><br>";
echo "<a href='index2.ph p'>Add another opening!</a><br>";
echo "<a href='show.php' >Show!</a><br>";
?>
Where the problem is when you click show (show.php) you see the serialized data.
I am trying to restore the data with an unserialize (see below line 8), but when I add that line of code, my data disappears instead of being stripped of the serialization. When I remove line 8, the data displays serialized.
So something is wrong, I guess with my unserialize method.
<?php
if($_COOKIE["mycookie"]) {
foreach( $_COOKIE[mycookie] as $key => $value)
{
$value2 = unserialize ($value);
echo "Number $key : Description $value2";
}
echo "<a href='index2.ph p'>Add Opening</a><br><br>";
}
else {
print "No Items in your cart<br><br>";
echo "<a href='index2.ph p'>Add Opening</a><br><br>";
}
?>
I have tried all weekend long and could use some help! Thanks
-Mr L8Knight
Comment