473,441 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

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 38143
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jas | last post by:
Hi All I guess I should just have tried this to see if it works but I don't have access to a machine running IIS and can't test it and I am itching for an answer. I want to use dynamic...
9
by: firegun9 | last post by:
Hello all, After looking for the solution for a while in the group, I know this is not possible in C++. So I just say my problme here. The input file is: dev1 1 1 dev2 0 0 0 6 There is a...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
5
by: Rolf Mander | last post by:
Hi, I need to use dynamic variable names but for objects. As you know something like that works fine: $variable="content"; $part="able"; echo ${"vari".$part}; // gives out content but i...
7
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.