Jump to content

Shopping cart in php- how to output all variables? (array?)


ethan6

Recommended Posts

Hello, I have a simple shopping cart in php and I have it set that when the user wants, they can click checkout and it brings them to paypal to pay.

 

Unfortunately, I can't seem to get all the variables to output in the "items" line, i can only get the last item in the cart (I think it's like in an array?)

 

Here's the code of the function showCart() that has the button to click on to go to paypal at the bottom:

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 action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM parts WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$part.'</td>';
		$output[] = '<td>&#36;'.$price.'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>&#36;'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>&#36;'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update cart</button></div>';
	$output[] = '</form><br>';
	$output[] = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">';
	$output[] = '<input type="hidden" name="cmd" value="_xclick">';
	$output[] = '<input type="hidden" name="business" value="Service@iTronicsRepair.com">';
	$output[] = '<input type="hidden" name="item_name" value="'.$part.'">';
	$output[] = '<input type="hidden" name="item_number" value="'.$id.'">';
	$output[] = '<input type="hidden" name="amount" value="'.$total.'">';
	$output[] = '<input type="hidden" name="shipping" value="2.99">';
	$output[] = '<input type="hidden" name="no_shipping" value="2">';
	$output[] = '<input type="hidden" name="return" value="http://itronicsrepair.com/thankyou_order.php">';
	$output[] = '<input type="hidden" name="cancel_return" value="http://itronicsrepair.com/cancel_order.php">';
	$output[] = '<input type="hidden" name="currency_code" value="USD">';
	$output[] = '<input type="hidden" name="tax" value="0.00">';
	$output[] = '<input type="hidden" name="lc" value="US">';
	$output[] = '<input type="hidden" name="bn" value="PP-BuyNowBF">';
	$output[] = '<input type="image" src="http://itronicsrepair.com/img/checkout.jpg" border="0" name="submit" alt="Make payments with PayPal - it is fast, free and secure!">';
	$output[] = '<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>

 

It is this line above where I want to output every part id#, not just the last one in the list:

		$output[] = '<input type="hidden" name="item_name" value="'.$part.'">';

 

Any thoughts? thanks.

 

oh if you want to view the cart, it is at http://itronicsrepiar.com/parts.php .. put something in the cart and then it brings you to http://itronicsrepair.com/cart.php

 

thanks.

 

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.