Jump to content

Dynamic Variables


creativkook

Recommended Posts

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.