473,395 Members | 1,679 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,395 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 38138
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
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.