Jump to content

PHP Shopping Basket


dachshund

Recommended Posts

Hey,

 

I'm having some problems getting my store website to show the contents of a shopping cart.

 

At the moment all the IDs of the items that have been added to the basket are being stored as $basket = $_SESSIONS['basket'].

 

This is the function I have to show those items, but it's not showing up anything. Can anywhere see the problem or offer some help?

 

Thanks

 


function showBasket() {
$basket = $_SESSION['basket'];
if ($basket) {
	$items = explode(',',$basket);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="basket.php?action=update" method="post" id="basket">';
	$output[] = '<table>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM store WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td><a href="basket.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$title.' by '.$author.'</td>';
		$output[] = '<td>£'.$price.'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>£'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update basket</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping basket is empty.</p>';
}
return join('',$output);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.