Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paypal Submission

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
Hi, I have set up my personal cart. I want the products selected to be itemised in paypal. All areas are being sent through to Paypal, the name, price and qty. However if there is more than one item, it only displays the last one put into the cart by the user. How do pass theses values to all items chosen?
My code is as follows:
Code:
<?php

	include("db.php");
		//switch statement like the if statement except it allows the condition to take more than two values. In an if statement the condition can be either true or false unless using curvy brackets, within a switch can take any number of different values.
	switch($_GET["action"])
	{
	//You need case statement to handle each value to react to, and default case to handle any that you do not provide a specific case statement for. So instead of multiple entries of elseif to find item, e.g if($find== '2') echo'vacuum1' elseif ($find=='3') etc you simply add the functions. 
		case "add_item":
		{
			AddItem($_GET["id"], $_GET["qty"]);
			ShowCart();
			break;
		}
		//When a case statement in a switch  is activated, php executes statements until it reaches a break statement. Without these a switch would execute all the code as true.
		case "update_item":
		{
			UpdateItem($_GET["id"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["id"]);
			ShowCart();
			break;
		}
		default:
		{
			ShowCart();
		}
	}

	function AddItem($itemId, $qty)
	{
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		$row = mysql_fetch_row($result);
		$numRows = $row[0];
		
		if($numRows == 0)
		{
			// This item doesn't exist in the users cart,
			// we will add it with an insert query

			@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
		}
		else
		{
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($itemId, $qty);
		}
	}
	
	function UpdateItem($itemId, $qty)
	{
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		if($qty == 0)
		{
			// Remove the item from the users cart
			RemoveItem($itemId);
		}
		else
		{
			mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		}
	}
	
	function RemoveItem($itemId)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart

		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
	}
	
	function ShowCart()
	{
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		$totalCost = 0;
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.item_name asc");
		?>
		<html>
		<head>
		<title> Your Shopping Cart </title>
		<script language="JavaScript">
		
			function UpdateQty(item)
			{
				itemId = item.name;
				newQty = item.options[item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
			}
		
		</script>
		<link href="vac.css" rel="stylesheet" type="text/css">
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
-->
</style>
</head>
		<body bgcolor="#ffffff">
		<div id="head">Your Shopping Cart</div>
		<div id="cleanercontent">
		<form name="frmCart" method="get"  action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr">[/URL]
		<input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="chris.n@vacuumshop.com.au">
		
		<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr class="green">
				<td width="15%" height="25">
						&nbsp;&nbsp;Qty
				</td>
				<td width="55%" height="25">
						Product
				</td>
				<td width="15%" height="25" >
						Price Each
				</td>
				<td width="15%" height="25" >
					&nbsp;Remove Item </td>
			</tr>
			<?php
			while($row = mysql_fetch_array($result))
			{
				// Increment the total cost of all items
				$totalCost += ($row["qty"] * $row["itemPrice"]);
				?>
					<tr>
						<td width="15%" height="25">
								<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
								<?php
								
									for($i = 1; $i <= 20; $i++)
									{
										echo "<option ";
										if($row["qty"] == $i)
										{
											echo " SELECTED ";
										}
										echo ">" . $i . "</option>";
									}
								?>
								</select>

								<input type="hidden" name="quantity_1" value="<?php echo $row["qty"];?>"
								>
						</td>
						<td width="55%" height="25">
								<?php echo $row["item_name"]; ?>
								
																<input type="hidden" name="item_name_1" value="<?php echo $row["item_name"]; ?>">

								
						</td>
						<td width="20%" height="25">
								$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
								<input type="hidden" name="amount_1" value="<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>">
						</td>
						<td width="10%" height="25">
								<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
							
						</td>
					</tr>
				<?php
			}
			
			// Display the total
			?>
					<tr>
						<td width="100%" colspan="4">
							<hr />
						</td>
					</tr>
					<tr>
						<td width="70%" colspan="2">
						</td>
						<td width="30%" colspan="2">
								<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
						</td>
					</tr>
		  </table>
		
		
		
  <tr>
  <td></td>
    <td>
     <a href = "javascript:history.back()"> <input type="button" name="Button" value="Keep Shopping"></a></td>
    <td>
<input type="hidden" name="currency_code" value="AUD">
<input type="hidden" name="item_name_$cnt" value="<?php echo $row["item_name"]; ?>"
								<?php
								$cnt =0;
			           while($row = mysql_fetch_array($item_name))
					   $cnt++;
			             {
						 $row["item_name"];
						 }
						 ?>>
<input name="submit" type="submit" value="Checkout" alt="Make payments with PayPal - it's fast, free and secure!"  border="0">
<img alt="" border="0" src="[URL unfurl="true"]https://www.paypal.com/en_AU/i/scr/pixel.gif"[/URL] width="1" height="1">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHFgYJKoZIhvcNAQcEoIIHBzCCBwMCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYCsRKZm5zmRuT1MO7U5LsM8Xk8pD38exxRXPH6bdcgbl1wkjMWsGGlM94ZsRW3Ahz+VvWvT+x3uxH30Fge5b61en12ODaG2gJxYHLtYv6RJ35JY/SVxOhVBowc003zX71aoQRyWbL46JvIy2jRWF09Acq0SofDremaKG4NmCe7JPTELMAkGBSsOAwIaBQAwgZMGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIEsu/YzqYxBSAcIlfMbUAdw8VS3OXdau9w2NswcT9SbTVDVpEn2U4ic06UyXPsNikejJGBM7Uue02HQRWQF7Stjowj9fn3pp1n1HGOJQlrLMKXYHLKWikTjFSW2ViLuCLOq+rzosAhztd0MK5JhpXKp2Uv0fM0bF62JagggOHMIIDgzCCAuygAwIBAgIBADANBgkqhkiG9w0BAQUFADCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERM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YDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tAgEAMAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0wNjA1MzEyMjU2MDhaMCMGCSqGSIb3DQEJBDEWBBTqtEL4q+jo/F6jl549atsbQtfu1DANBgkqhkiG9w0BAQEFAASBgJVIMczfjcebA3TkGPlq1uP2Q2xNPuyvZEQ4SrFMM75wnPLMiM+hZ0wMJc2hhB4yiWh1/O4DWKPTefk39cPKsRQaoDuFpeeINiP+eBmVKeMtuLNgmfSnxap1bN8EAneJaedy7mcqNdfV33poRPHU5MthAq7l7N/KLcWu0GRz8ZGi-----END PKCS7-----
">

</form></td>
  </tr>
</div>
		</body>
</html>
			<?php
	}

?>
 
i've never seen curly braces inside a case of switch statement. i'm surprised it works without a parse error.

the problem you have posted about, however, is occurring because you are naming the relevant form field [ ]_$cnt. ie you are not appending the variable $cnt but the literal string "$cnt". you need to put the form field name inside php tags
Code:
<input type="hidden" name="<?php echo "item_name_$cnt";?>  value="<?php echo $row["item_name"]; ?>"

I also suspect that the above lines should be inside a loop as you need to push out the value, name and quantity of each item for the paypal cart. the loop that you have inserted immediately after this code does not do anything at all (which will also be problem for you)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top