Dragonfly CMS v9 ⇒ Modules & Blocks :: Archives ⇒ PHP-Specific Question about variables :: Archived ⇒ Community Forums ⇒ CPG Dragonfly™ CMS
Forum IndexModules & Blocks

Archived ⇒ PHP-Specific Question about variables


Let's say I have a few variables: $item_number1, $item_number2, $item_number3, etc. These variable names cannot be changed, as they are sent via another website.

Since I won't know how many items I may have, I want to put those variables into an array using a loop.

How do I take care of that increasing # on the end of each variable name?

I tried this, but the code would take the value of $item_number (which is undefined) and then join the index of the loop to the end of that.
$num_items = the total number of items I have; $array_items = array(); //loop through all items (1 to num_items) for ($i = 1; $i <= $num_items; $i++){ $array_items[$i] = $item_number.$i; }

Any help would be greatly appreciated. I wouldn't know where to look in the PHP manual for the answer

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.33/4.0.27/4.4.4/9.1.1


Variables don't exist when they come from another website.

You can however pass variables from one website to another using $_POST or $_GET.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


Sorry, yes, they were passed via the $_GET method. I guess I should explain some background information on this code...

PayPal has a feature called Payment Data Transfer and it allows you to make payments through PP (from your website). As the transaction is being processed, paypal's server will send data to a script that you have which will allow you to keep track of customer order information or other automated junk.
www.paypal.com/Integra...c_pdt.html

Basically, they send me data containing the variables $item_number1, $item_number2, etc for each item that was purchased from my website. I just want to know how to put each of those variables into an array. And since I won't know how many possible cart items there may be, I need to use a loop that increases the # in that variable by 1 each time. I just don't know how call that variable with the loop's index number attached to it.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.33/4.0.27/4.4.4/9.1.1


www.pdncommunity.com/p...ard.id=pdt

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):


Well, I ended up just reading through a bunch of the PHP manual and a user submitted an example that helped fix my problem.

This Works ...
$pp_num_cart_items = 3; $pp_item_number1 = "Item 1"; $pp_item_number2 = "Item 2"; $pp_item_number3 = "Item 3"; $cart_items = array(); for ($i = 1; $i <= $pp_num_cart_items; $i++){ $varname = sprintf("pp_item_number%d",$i); $cart_items[$i] = $$varname; } print_r($cart_items); //Array ( [1] => Item 1 [2] => Item 2 [3] => Item 3 )

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.33/4.0.27/4.4.4/9.1.1

All times are UTC