Loading Variable Variables into a TwoDimensional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ciphex
Forum Newbie
Posts: 3
Joined: Sat Dec 11, 2004 2:08 pm

Loading Variable Variables into a TwoDimensional Array

Post by ciphex »

Hey guys,
I am pretty new to PHP and am trying to do something that i cannot find specific definition for anywhere. It goes like this:

I have variables from a form... alot of them.
They correspond to editable fields of a MySQL sourced table displayed on the previous page.

I am trying to compare these form submitted variables to the original table data which i load into an array.

I am trying to get these submitted variables into a a matching multidimensional array for easy comparison (need to determine the differences of each field).

Now the submitted variables have a naming scheme which will lend itself to my endeavor.

i am trying to fill a two-dimensional array,via a for loop, one "row" at a time with the variables. However i need to specify the names of the variables for each "row" using the iteration counter of my loop. But it does not seem to be working. Sorry if i am not explaining this well... perhaps the code will help explain better.



Code: Select all

for ( $i=0; $i<$eNumRecords; $i++)
						&#123;
						$Wines = "Wines_".$i;
						$CostPerBottle = "CostPerBottle_".$i;
						$OnHand = "OnHand".$i;
						$Sold = "Sold".$i;
						$Price = "Price".$i;
						$Shipping_2 = "Shipping_2".$i;
						$Shipper_2 = "Shipper_2".$i;
						$Pay_Pal = "Pay_Pal".$i;
						$Sales_Tax = "Sales_Tax".$i;
						$PalPay = "PalPay".$i;
						$Count = "Count".$i;
						$Expense = "Expense".$i;
						$WC_Comm = "WC_Comm".$i;
						
						
							$Modified .= array( array( Wines => $$Wines,
													  CostPerBottle => $$CostPerBottle,
													  OnHand => $$OnHand,
													  Sold => $$Sold,
													  Price => $$Price,
													  Shipping_2 => $$Shipping_2,
													  Shipper_2 => $$Shipper_2,
													  PayPal => $$Pay_Pal,
													  Sales_Tax => $$Sales_Tax,
													  PalPay => $$PalPay,
													  Count => $$Count,
													  Expense => $$Expense,
													  WC_Comm => $$WC_Comm
													  ));
						&#125;
echo $Modified&#1111;0]&#1111;"Price"];

Now... that last echo command should (in my mind) print out the value of the variable which came in via the form as $Wines_0
Placing

Code: Select all

echo $Wines_0;
before and after that loop returns the correct value but i cannot seem to get it or the rest of the variables into the array.
This would not be such a big deal if the number of rows was static but it is not so getting over this hump is essential to my design.

Do I not have a correct understanding of $$varname's or perhaps My Array is not being built properly ( .= )???

Any light you guys could shed would be most helpful.
For better or for worse even.. just so i know.
It seems to me this is just the kind of thing that $$varname's would be used for.

But again... I'm new to this.
Thanx!

John

(In case you are wondering why I would do such a thing I am porting an excel spreadsheet to PHP sourcing MySQL)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Try this nice package to export whatever u want

Very nice Stuff Right Here
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

.= is a string operator. you cant(directly) use it w/ arrays

try replacing
$Modified .=

w/
$Modified[] =

each time you do the above in your loop, it will add a new element to the array, and it will be numerically indexed, starting at 0

its kinda hard to understand how your trying to do this, but if your trying to build a big 2 dim array, my suggestion should work.

also, print_r() is nice for debugging when working w/ arrays :)


you also might find this easier than doing the variable variables thing your doing

Code: Select all

for (...) {

$modified[] = array(
       $_POST["wines_$i"],
       $_POST["cost$i"],
        ); // etc....
}
ciphex
Forum Newbie
Posts: 3
Joined: Sat Dec 11, 2004 2:08 pm

Post by ciphex »

Thanx ol4... looks like that could come in handy in the right situation.

However I develop under OS X and my webhosts are all debian servers.
Also the reason for this port is extensibility, remote access, and platform independance. So while i can see that BadBlue and its counterparts are handy tools sadly not for me. But I am used to it :-)

rehfeld...
that explains a good bit then.
one question though.. what exactly is going on in

Code: Select all

$_POST&#1111;"wines_"],
what i need to happen is for the $_POST (identifier) to be defined staticly
and the ["wines_$i"] to be "wines_".$i

is this the case? if so what do you call this operation and where might i read more about it?

Thanx so much for your time guys!
ciphex
Forum Newbie
Posts: 3
Joined: Sat Dec 11, 2004 2:08 pm

Post by ciphex »

i think i have answered my own question here. after taking a closer look and all. php is a bit different than languages i am used to (not many) but i am growing to like it a lot.

thanx again
im sure ill be posting here plenty :-)

John
Post Reply