Jump to content

view cart - help me strip out some code!


agazad

Recommended Posts

Hi,

 

had a quick search but couldn't find the exact solution.  I'm new-ish to PHP and have been struggling with this for hours now  ???

 

The function below shows the contents of my basket (i.e. displays the items in the basket, and the quantities (product ids are stored in a session).  I need to copy this function, and modify it so that it outputs a list of *all* the products in the session, disregarding quantities (i.e. if you have two of something in the basket, I want it to display the same details twice as opposed to display one with a quantity box).  It's a case of stripping some of the functionality from this function - but I'm not clever enough to know what  ::)

 

Any help much appreciated. Code is from a brilliant simple PHP shop by the watchmaker project.

 

function showBasket() {
global $db;
$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 products 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.' - '.$catalogue.'</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

Hi

 

try this, i just ran through it quickly, hopefully got it right...

 

function showBasket() {
<?php

global $db;
$basket = $_SESSION['basket'];
if ($basket) {
	$items = explode(',',$basket);
	$output[] = '<form action="basket.php?action=update" method="post" id="basket">';
	$output[] = '<table>';
	foreach ($basket as $id) {
		$sql = 'SELECT * FROM products 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.' - '.$catalogue.'</td>';
		$output[] = '<td>£'.$price.'</td>';
		$total += $price;
		$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);
}

 

cheers,

tdw

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.