Jump to content

Need Help with Session Arrays and displaying values


brettc

Recommended Posts

Hi guys, I'm very new to PHP and I'm having trouble getting one of my arrays to display correctly.

 

I'm trying to make a standard shopping cart for an assignment, and I can add 1 item easily with no problems, but when I add a 2nd or third I'm finding display problems. First of all here are my codes.

 

1st code block

$item = $_POST['itemdisplay'];
$itemquery = mysql_query("SELECT * FROM items WHERE item_name='$item'");
$item_name = mysql_result($itemquery, 0, 'item_name');
$item_price = mysql_result($itemquery, 0, 'item_price');
$item_desc = mysql_result($itemquery, 0, 'item_desc');
$cat_id = mysql_result($itemquery, 0, 'cat_id');

$sizequery = mysql_query("SELECT size FROM item_size WHERE cat_id='$cat_id'");

$displayitems = array($item_name, $club, $item_price);
$_SESSION['displayitems'] = serialize($displayitems);

while($size = mysql_fetch_array($sizequery))
{
$displayblock .= "<OPTION VALUE=".$size['size'].">".$size['size']."</OPTION>";
}

?>

</HTML>



<BODY>
<?php include("header.php"); ?>
<TABLE>
<TR>
	<TH WIDTH="20">
	<TH WIDTH="150">Name
	<TH WIDTH="150">Club
	<TH WIDTH="150">Price
	<TH WIDTH="150">Sizes
	<TH WIDTH="250">Image
</TR>
<FORM method="POST" action="sportal_addcart.php">
<TR>
	<TD>
	<TD ALIGN="Center"> <?php echo $item_desc ; ?>
	<TD ALIGN="Center"> <?php echo $club ; ?>
	<TD ALIGN="Center"> $<?php echo $item_price ; ?>
	<TD ALIGN="Center"><SELECT NAME="size"><?php echo $displayblock ; ?></SELECT>
</TR>
<TR>
	<TD>
	<TD ALIGN="Center"><BR><input type="submit" value="Add to cart">
</TR>


</TABLE>
<?php include("navbar1.php"); ?>
</BODY>
</HTML>

 

The $_SESSION['displayitems'] array is then posted to my addcart page.

<?php
session_start();

$itemsize = $_POST['size'];

$displayitems = unserialize($_SESSION['displayitems']);
$cartitems = array($displayitems[0], $displayitems[1], $displayitems[2], $itemsize);

if (!empty($_SESSION['cart'])) {
$products = array_merge(unserialize($_SESSION[cart]),
		$cartitems);
$_SESSION['cart'] = serialize($products); }
else 
{$_SESSION['cart'] = serialize($cartitems);}

echo "Item has been added to your Cart<BR>";

include("navbar1.php");
?>

 

and finally the Show Cart page;

 

<?php
session_start();

if (!$_SESSION['cart']) {
echo "There are no items in your cart";
}
else {		echo "<TABLE>
				<TR>
				<TH>Item Name
				<TH>Club
				<TH>Price
				<TH>Size
			</TR>
			<TR>";
foreach (unserialize($_SESSION['cart']) as $p) {
		echo	"<TD>$p";}
		echo	"</TR></TABLE>";	

	echo "<br><a href='sportal_resetcart.php'>Clear Cart</a>";
}
echo "<BR><BR>";
include("navbar1.php");

?>

 

I think the problem is;

- each entry has 4 values, but each 4 is being stored as its own separate array entry.

- when a 2nd entry is added it becomes 8 stored entries, instead of 2 entries with 4 values respectively... if that makes sense?

Hi, could you try this on your show cart page:

 

<?php
session_start();

if (!$_SESSION['cart']) {
echo "There are no items in your cart";
}
else {		echo "<TABLE>
				<TR>
				<TH>Item Name</TH>
				<TH>Club</TH>
				<TH>Price</TH>
				<TH>Size</TH>
			</TR>
			<TR>";
foreach (unserialize($_SESSION['cart']) as $p) {
		echo	"<TD>$p</TD>";}
		echo	"</TR></TABLE>";	

	echo "<br><a href='sportal_resetcart.php'>Clear Cart</a>";
}
echo "<BR><BR>";
include("navbar1.php");

?>

 

Forgive me if I'm wrong, but I think you've screwed up your table. You didn't close your TH tags or your TD tags. Also, I'm only seeing the variable $p being printed out?

Thanks for the reply.

 

I tried it out but still having the problem, the TH and TD tags can usually stay open too.  :-\

 

The $p tag cycles through each array item in  $_SESSION[cart] and displays them one by one.

 

However as you can see, by the number row headers, I have 4 columns, when I add 1 entry to the cart those 4 columns are filled with the first entry. What I need is for it to go to the next row for the next 4 values are used....and then again after 8, and so on.

Here's what the output looks like after adding one item

 

Item Name    Club    Price  Size

--------------------------------

Jumper      Richmond 39.99 Small

 

Which is ok but each subsequent line doesn't start on a new row.

 

Item Name    Club    Price  Size

--------------------------------

Jumper      Richmond 39.99 Small  Socks  Collingwood  19.99 Large

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.