Dynamic Variable Names

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] ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-03-2003
Michael J. Astrauskas
 
Posts: n/a
Default 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.

Reply With Quote
  #2 (permalink)  
Old 10-04-2003
127.0.0.1
 
Posts: n/a
Default Re: Dynamic Variable Names

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>
Reply With Quote
  #3 (permalink)  
Old 10-04-2003
Timo Henke
 
Posts: n/a
Default Re: Dynamic Variable Names

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

Reply With Quote
  #4 (permalink)  
Old 10-05-2003
Nikolai Chuvakhin
 
Posts: n/a
Default Re: Dynamic Variable Names

"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
Reply With Quote
  #5 (permalink)  
Old 10-05-2003
Michael J. Astrauskas
 
Posts: n/a
Default Re: Dynamic Variable Names

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:26 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0