469,267 Members | 861 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Dynamic Variable Names

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.

Jul 17 '05 #1
4 37918
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)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #2
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

Jul 17 '05 #3
"Michael J. Astrauskas" <tr****@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
Jul 17 '05 #4
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

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jas | last post: by
9 posts views Thread by firegun9 | last post: by
5 posts views Thread by Rolf Mander | last post: by
7 posts views Thread by DavidSeck.com | last post: by
26 posts views Thread by Aaron \Castironpi\ Brady | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.