Cart help needed

I am working on a shopping cart with PayPal but im having some trouble

I have a function

function showCart() {
	global $db;
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<form name="PayPal" method="post" action="https://www.paypal.com/cgi-bin/webscr">';					
		$output[] = '<input type="hidden" name="cmd" value="_cart">';
		$output[] = '<input type="hidden" name="business" value="[email protected]">';
		$output[] = '<input type="hidden" name="upload" value="1">';
		$output[] = '<input type="hidden" name="currency_code" value="GBP">';
		$output[] = '<input type="hidden" name="page_style" value="aSweb">';
		?>
		<table cellpadding="3" cellspacing="2" bgcolor="#FFFFFF">
		  <tr bgcolor="#2F4F4F">
		    <th width="200" align="left" style="color:#FFFFFF">Description</th>
			<th width="50" align="right" style="color:#FFFFFF">Cost</th>
			<th width="75" align="right"></th>
		  </tr>
		
		<?
		
		foreach ($contents as $id=>$qty) {
			$sql = 'SELECT * FROM downloads WHERE id = '.$id;
			$result = $db->query($sql);
			$row = $result->fetch();
			extract($row);
			$output[] = '<tr bgcolor="#F0FFFF">';
			$output[] = '<td>'.$title.'</td>';
			$output[] = '<td align="right">&pound;'.$amount.'</td>';
			$total += $amount;
			$output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="../styles/images/delBtn.gif" alt="Remove from cart" border="0px" /></a></td>';
			$output[] = '</tr>';
			$output[] = '<input type="text" name="item_name'.$item.'" value="'.$title.'">';
			$output[] = '<input type="text" name="item_number'.$item.'" value="'.$id.'">';
			$output[] = '<input type="text" name="amount'.$item.'" value="'.$amount.'">'; 
			}
		
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';   
	    $output[] = '<input type="hidden" name="no_shipping" value="2">';
	    $output[] = '<input type="hidden" name="no_note" value="1">';
	    $output[] = '<input type="hidden" name="bn" value="IC_Sample">';
	    $output[] = '<input type="submit" name="submit" value="Check Out">';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}

This code works but when I click on the checkout button its not sending anything to paypal

When I alter

$output[] = '<input type="text" name="item_name'.$item.'" value="'.$title.'">';
$output[] = '<input type="text" name="item_number'.$item.'" value="'.$id.'">';
$output[] = '<input type="text" name="amount'.$item.'" value="'.$amount.'">'; 

to

for($i=1;$i < count($items);$i++) {
$output[] = '<input type="text" name="item_name'.$item.'" value="'.$title.'">';
$output[] = '<input type="text" name="item_number'.$item.'" value="'.$id.'">';
$output[] = '<input type="text" name="amount'.$item.'" value="'.$amount.'">'; 
}

Its passing information to PayPal but its multiplying the items

Anyone have any suggestions, I know there is a way round it but I cant see the wood for the trees, just need someone to point out what i’m missing

You need to specify which array element you’re writing each value to. Also if you used $i < count() as your control then you probably meant to set $i=0, not $i=1. Something like:

for($i=0;$i < count($items);$i++) { $output[$i] = '<input type="text" name="item_name'.$item.'" value="'.$title.'">'; $output[$i] = '<input type="text" name="item_number'.$item.'" value="'.$id.'">'; $output[$i] = '<input type="text" name="amount'.$item.'" value="'.$amount.'">'; }

Sorted it

$itemCount=1;
foreach ($contents as $id=>$qty) {
         $sql = 'SELECT * FROM downloads WHERE id = '.$id;
         $result = $db->query($sql);
         $row = $result->fetch();
         extract($row);
         $output[] = '<tr bgcolor="#F0FFFF">';
         $output[] = '<td>'.$title.'</td>';
         $output[] = '<td align="right">&pound;'.$amount.'</td>';
         $total += $amount;
         $output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="../styles/images/delBtn.gif" alt="Remove from cart" border="0px" /></a></td>';
         $output[] = '</tr>';
         $output[] = '<input type="text" name="item_name_'.$itemCount.'" value="'.$title.'">';
         $output[] = '<input type="text" name="item_number_'.$itemCount.'" value="'.$id.'">';
         $output[] = '<input type="text" name="amount_'.$itemCount.'" value="'.$amount.'">'; 
$itemCount++;
         }
Sponsor our Newsletter | Privacy Policy | Terms of Service