Jump to content

php loop-- how to get all data from arrays?


ethan6

Recommended Posts

I have this bit of code that gets the info from a MySQL database and displays it, as well as puts it into a PayPal button. 

 

It's from a shopping cart, so basically if the customer has 3 of product #1 and 2 of product #2, I need to know all of this information in the paypal button, but currently it only gives like the first or last iteration of information(?) sorry i'm not really sure how this works. 

 

so here's the code:

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>';
	$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="[email protected]">';
	$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://myipodbroke.com/img/service_button.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's the variables $part, $qty of parts, and $id that I need when the code writes the paypal button, and I'm not sure how to do this.

 

Thank you in advance!

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.