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 strongm 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
I know I have posted this question before. I have a cart that I need to pass the values to paypal. All the single values are passed, however when I have more than one product in my shopping cart it does not pass every product. It only passes the last one selected. Do I need to wrap a loop around the code? Please help! ..........again.
The red highlighted area is what is sent to paypal.
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">
		[COLOR=red]<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">[/color]
		
		<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>

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

								
						</td>
						<td width="20%" height="25">
								$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
								[COLOR=red]<input type="hidden" name="amount_1" value="<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>">[/color]
						</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>
[COLOR=red]<input type="hidden" name="currency_code" value="AUD">

<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+rzosAhztd0MK5JhpXKp2Uv0fM0bF62JagggOHMIIDgzCCAuygAwIBAgIBADANBgkqhkiG9w0BAQUFADCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDX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IGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tAgEAMAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0wNjA1MzEyMjU2MDhaMCMGCSqGSIb3DQEJBDEWBBTqtEL4q+jo/F6jl549atsbQtfu1DANBgkqhkiG9w0BAQEFAASBgJVIMczfjcebA3TkGPlq1uP2Q2xNPuyvZEQ4SrFMM75wnPLMiM+hZ0wMJc2hhB4yiWh1/O4DWKPTefk39cPKsRQaoDuFpeeINiP+eBmVKeMtuLNgmfSnxap1bN8EAneJaedy7mcqNdfV33poRPHU5MthAq7l7N/KLcWu0GRz8ZGi-----END PKCS7-----
">[/color]

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

?>
 
please don't post your paypal certificates. it completely breaks the tek-tips css.

answer to your question: you have got a loop in there. you don't need another one.

the problem: you are naming all your elements with a suffix of "_1". as you have previously posted and had answered, these suffixes need to increment.

change your while loop for the following code:
Code:
            while($row = mysql_fetch_array($result))
            {
                (if (!isset($cnt)) { $cnt =0;}
				// 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_<?php echo $cnt;?>" value="<?php echo $row["qty"];?>">
                        </td>
                        <td width="55%" height="25">
                                <?php echo $row["item_name"]; ?>
                                
                                 <input type="hidden" name="item_name_<?php echo $cnt;?>" value="<?php echo $row["item_name"]; ?>">

                                
                        </td>
                        <td width="20%" height="25">
                                $<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
                                <input type="hidden" name="amount_<?php echo $cnt;?>" 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
				$cnt++;
            }
 
The (if (!isset($cnt)) { $cnt =0;} seems to stop the cart from showing at all. Nothing appears
 
I have tried separating the cart and using the paypal coding as a separate form, but still nothing shows. What is this?
 
Code:
<form name="paypal" method="post"  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="3">
        <input type="hidden" name="business" value="info@vac.com.au">
		 <?php
		while($row = mysql_fetch_array($result))
            {
                (if (!isset($cnt)) { $cnt =0;}
                // Increment the total cost of all items
                $totalCost += ($row["qty"] * $row["itemPrice"]);
                ?>                               
<input type="hidden" name="quantity_<?php echo $cnt;?>" value="<?php echo $row["qty"];?>">
 <input type="hidden" name="item_name_<?php echo $cnt;?>" value="<?php echo $row["item_name"]; ?>">
<input type="hidden" name="amount_<?php echo $cnt;?>" value="<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>">
                       
                <?php
                $cnt++;
            }?>
<input type="hidden" name="currency_code" value="AUD">

<input name="submit" type="submit" value="Checkout" alt="Make payments with PayPal - it's fast, free and secure!"  border="0">
</form>
 
do you have error_reporting switched on
Code:
error_reporting(E_ALL);
and if so what error message are you receiving?

also change this line:
Code:
(if (!isset($cnt)) { $cnt =0;}
to
Code:
if (!isset($cnt)) { $cnt =0;} // remove extraneous open bracket

this error should have been apparent to you if you errors were being reported. have you read sleipnir's FAQ on debugging?
 
No error appears at all. No I have no read about that. Will this help me figure out what is happening?
 
can I create two separate loops in one while loop
 
So, has this been resolved?
If not, where lies the issue?

And yes, you can have as many nested loops as you wish.

ie

while(1) {
while(2) {
while(3) {
something
}
}
while(4) {
something else
}
}

etc

JR
IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
i am unaware of the resolution on this thread. i received no *substantive* reply to my last post.
 
I am unsure about how i find the problem, you suggest debugging?? The cart now is visible on the server with the () brackets removed from the if statement. However it is still not looping through to pass item_name_1, item_name_2 etc to paypal.
Because there are two loops in this,
Code:
<?php
			while($row = mysql_fetch_array($result))
			{
		if (!isset($cnt)) { $cnt =1;}
				// Increment the total cost of all items
				$totalCost += ($row["qty"] * $row["itemPrice"]);
				?>
should there be another while () added? I really do not know what the problem is, or how to find it? Sorry to be so vacant, every step is new to me.
 
please post the source code generated by your script (i.e. use view source on a small shopping cart and post the result).
 
I finally figured it out, I simply did not have the $cnt incrementing. $cnt++
Thankyou so much for your time and patience, I know I must have been a pain! Thanks heaps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top