This is a discussion on Dynamic Variable Names within the PHP Language forums, part of the PHP Programming Forums category; I have a shopping cart page that allows a user to type in a new quantity and press a [Submit] ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a shopping cart page that allows a user to type in a new
quantity and press a [Submit] button. The quantities entered are in form text boxes with the names "item302", "item7812", etc. reflecting which item that particular textbox is associated to. I store the entered variables using 'import_request_variables("p","p_")' so I end up with $item302, $item7812, etc. My script looks for these variables by looking through the shopping cart array which stores the item numbers themselves and then checks the value of $item<whatever>. If I have a number stored in $item is there any easy way to tell PHP to check the value of variable $p_item<item>? For example, if $item is '302' how can I tell PHP to evaluate $p_item302? Thank you! -- - Michael J. Astrauskas P.S. I'm currently doing this using the eval() function, but I don't like that technique. |
|
|||
|
Michael J. Astrauskas wrote:
> For example, if $item is '302' how can I tell PHP to evaluate > $p_item302? Try: $varname = sprintf("p_item%d", $item); echo($$varname) -- Spam:newsgroup(at)craznar.com@verisign-sux-klj.com EMail:<0110001100101110011000100111010101110010011 010110 11001010100000001100011011100100110000101111010011 011100 11000010111001000101110011000110110111101101101001 00000> |
|
|||
|
127.0.0.1 wrote:
> Michael J. Astrauskas wrote: > >> For example, if $item is '302' how can I tell PHP to evaluate >> $p_item302? > Try to define a dynamic Var: $item = "ABC"; ${"p_item{$item}"} = "value of this var"; print $p_itemABC; would output "value of this var"; greetings .. timo |
|
|||
|
"Michael J. Astrauskas" <trevie@cox.net> wrote in message
news:<himfb.46312$vj2.21136@fed1read06>... > > if $item is '302' how can I tell PHP to evaluate $p_item302? Use a variable variable: $name = 'p_item' . $item; $value = $$name; Since $name evaluates to 'p_item302', $$name must evaluate to $p_item302. See the Manual for details: http://www.php.net/variables.variable Cheers, NC |
|
|||
|
Nikolai Chuvakhin wrote:
> Michael J. Astrauskas" wrote: > >> if $item is '302' how can I tell PHP to evaluate $p_item302? > > Use a variable variable: > > $name = 'p_item' . $item; > $value = $$name; Thank you! I combined what you and Timo Henke suggested and it works beautifully! Here is my final code: $v = ${'p_ITEM'.$key}; -- - Michael J. Astrauskas |