creativkook Posted June 27, 2007 Share Posted June 27, 2007 Ok, I know there's a way to have variables be created dynamically. What I'm trying to do is to pass information about products to PayPal, but I don't know how many products are going to be in each cart. It could be 1 or 100. I need to create a variable who's ending number changes depending on how many products are in the cart. How do you have those created dynamically? I need the information to be passed through hidden variables via an HTML form as well. Quote Link to comment Share on other sites More sharing options...
per1os Posted June 27, 2007 Share Posted June 27, 2007 Why not just use an array? Anyrate to create a variable on the fly you use $$ IE: <?php $var_name = "my_var"; for ($i=0; $i<5; $i++) { $var = $var_name . $i; $$var = "product" . $i; } echo $my_var1 . " " . $my_var2; // ..etc ?> Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 27, 2007 Author Share Posted June 27, 2007 Well, the question is I need to put this inside a while loop. The Code: if(mysql_num_rows($carts) == 0) { echo("<tr><td rowspan='4'>You have not yet put anything in your cart.</td></tr>"); } else { while($cart = mysql_fetch_array($carts)) { $products = mysql_query("SELECT title, price FROM products WHERE productsid = $cart[productid]"); while($product = mysql_fetch_array($products)) { echo("<tr>"); echo("<td valign='top'>$product[title]</td>"); echo("<td valign='top'>\$$product[price]</td>"); echo("<td valign='top'>$cart[quantity]</td>"); echo("</tr>"); [b]echo("<input type='hidden' name='item_name_1' value='$product[title]'>")[/b] } } } So, I need the bolded line to have a different name for each product. If there's more than one item, it needs to be able to change in the loop to create another hidden input with the name item_name_2, etc. How do I make it do that? Quote Link to comment Share on other sites More sharing options...
trq Posted June 27, 2007 Share Posted June 27, 2007 You dont even want a different name. Just make that into an array aswell. eg; echo "<input type='hidden' name='items[]' value='$product['title']'>"; Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 27, 2007 Author Share Posted June 27, 2007 So that's the only code I need to put in, is the item_name_[]? Because It has to have the name of item_name_#. Quote Link to comment Share on other sites More sharing options...
trq Posted June 27, 2007 Share Posted June 27, 2007 That will create an array of items in $_POST['items']. Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 27, 2007 Author Share Posted June 27, 2007 Ok, I know I sound dumb. I've done this before, but I don't remember exactly how it works. Do I need to do anything special other than add the brackets to pull the information that PayPal needs? Quote Link to comment Share on other sites More sharing options...
trq Posted June 27, 2007 Share Posted June 27, 2007 Do I need to do anything special other than add the brackets to pull the information that PayPal needs? Wouldn't have a clue. What do paypall expect your data to look like? Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 28, 2007 Author Share Posted June 28, 2007 Ok, I got the variables to work correctly. But the problem I'm having now is I can't get the value of the hidden variable to reflect the product title. Here's the code I have now, but it's creating two hidden inputs of each, one for each of the amount in the shopping cart. Here's what it's doing: <tr> <td valign='top'>Wavy Fish</td> <td valign='top'>$20</td> <td valign='top'>1</td> </tr> <input type='hidden' name='item_name_1' value='Wavy Fish'> <input type='hidden' name='amount_1' value='20'> <input type='hidden' name='quantity' value='1'> <input type='hidden' name='shipping_1' value ='0.00' /> <input type='hidden' name='item_name_2' value='Wavy Fish'> <input type='hidden' name='amount_2' value='20'> <input type='hidden' name='quantity' value='1'> <input type='hidden' name='shipping_2' value ='0.00' /> <tr> <td valign='top'>Opal</td> <td valign='top'>$20</td> <td valign='top'>1</td> </tr> <input type='hidden' name='item_name_1' value='Opal'> <input type='hidden' name='amount_1' value='20'> <input type='hidden' name='quantity' value='1'> <input type='hidden' name='shipping_1' value ='0.00' /> <input type='hidden' name='item_name_2' value='Opal'> <input type='hidden' name='amount_2' value='20'> <input type='hidden' name='quantity' value='1'> <input type='hidden' name='shipping_2' value ='0.00' /> But it should assign item_name_1 to the Wavy fish product and item_name_2 to Opal. Here's the code behind: $count = mysql_num_rows($carts); while($cart = mysql_fetch_array($carts)) { $products = mysql_query("SELECT productsid, title, price FROM products WHERE productsid = $cart[productid]"); while($product = mysql_fetch_array($products)) { echo("<tr>"); echo("<td valign='top'>$product[title]</td>"); echo("<td valign='top'>\$$product[price]</td>"); echo("<td valign='top'>$cart[quantity]</td>"); echo("</tr>"); for($x = 1; $x <= $count; $x++) { $name = "item_name_" . $x; $amount = "amount_" . $x; $shipping = "shipping_" . $x; echo("<input type='hidden' name='$name' value='$product[title]'>"); echo("<input type='hidden' name='$amount' value='$product[price]'>"); echo("<input type='hidden' name='quantity' value='1'>"); echo("<input type='hidden' name='$shipping' value ='0.00' />"); } } } } Anyone have any ideas? I keep moving the for() loop out of the while() loop, but I can't get the product title to come through and then the input has no value. Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 28, 2007 Share Posted June 28, 2007 With all the trouble you are going through to use PayPal's superslow cart ???, why don't you just make your own custom cart and then pass one simple order number to PayPal? You really are not not far away from a programming standpoint, and your cart would be much better, too! Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 28, 2007 Author Share Posted June 28, 2007 Well, the problem is I need PayPal to process the credit cards. What I'm doing here is passing the information from my cart to PayPal so that they can process the credit card. The customer wants each item to show up in the PayPal system so there's no confusion about which items are being purchased. Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 28, 2007 Share Posted June 28, 2007 Try making the entire string itself first. Like this: $itemName="testItem_"; $itemName.="$yourItemNumberHere"; Then, put that in the value box. Quote Link to comment Share on other sites More sharing options...
creativkook Posted June 29, 2007 Author Share Posted June 29, 2007 I did try that, and I tried making it a global variable. It only pulls out one. And in order for it to work with PayPal's thing, the name of each has to be item_name_1, item_name_2, in order, etc. Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 Are you positive that they must be in order? When I used PayPal, I do not remember that being the case. If possible, put a lot of debugging stuff in there with print statements. Like, "Starting loop 1--count is $count." Then, post it. The more print statements for things the better. When I am really stuck, I do that, and I usually find the solution. Because two is a bit odd, add another item to the cart so you have three in there. That might make it easier as well. Then, put the output with the debugged stuff here as well as what SHOULD be there. Also, just to make sure, explain what $carts and $count are. I am assuming that $cart is just a single cart and $count is the number of items in that cart. Is that right? If you always loop $count times, then, you are always going to get $count lines. So, if you had ten items in the cart, every item would have ten listings. Since you know how many items are in the cart as well, it would probably be much easier to make a for loop for this. IF you can, post a link to this. I would like to add items, etc., and look at the source. It is difficult sometimes for me to troubleshoot static code like this, lol. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.