473,503 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting a varialble using a variable

jj
Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable: $node-
>field_screen_1_caption[0]['view']. The loop should run n times,
incrementing that number 1 so next variable is $node-
>field_screen_2_caption[0]['view'].
Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;
}

Doesn't work obviously. Any suggestions for a PHP amateur.

Thanks,
jj
Nov 27 '07 #1
8 1488
On Nov 27, 8:40 pm, jj <jjwalke...@gmail.comwrote:
Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable: $node->field_screen_1_caption[0]['view']. The loop should run n times,

incrementing that number 1 so next variable is $node-
field_screen_2_caption[0]['view'].

Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;

}

Doesn't work obviously. Any suggestions for a PHP amateur.

Thanks,
jj
eval() - http://php.net/eval

for ($i = 1; $i <= $number_of_screens; $i++) {
eval("\$cap = \$node->field_screen_{$i}_caption[0][\"view\"];");
print $cap;
}

--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #2
jj
Excellent. Thanks, Kailash! I wouldn't have known what to look for.
jj
eval() -http://php.net/eval

for ($i = 1; $i <= $number_of_screens; $i++) {
eval("\$cap = \$node->field_screen_{$i}_caption[0][\"view\"];");
print $cap;

}
Nov 27 '07 #3
On Tue, 27 Nov 2007 21:40:24 +0100, jj <jj********@gmail.comwrote:
Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable: $node-
>field_screen_1_caption[0]['view']. The loop should run n times,
incrementing that number 1 so next variable is $node-
>field_screen_2_caption[0]['view'].

Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;
}

Doesn't work obviously. Any suggestions for a PHP amateur.
echo $node->{'field_screen_'.$i.'_caption'}[0]['view']

It has some serious flaws in design if you have to use it like that though.
--
Rik Wasmus
Nov 27 '07 #4
On Nov 27, 9:18 pm, jj <jjwalke...@gmail.comwrote:
Excellent. Thanks, Kailash! I wouldn't have known what to look for.
;) np. You're welcome!
jj
eval() -http://php.net/eval
for ($i = 1; $i <= $number_of_screens; $i++) {
eval("\$cap = \$node->field_screen_{$i}_caption[0][\"view\"];");
print $cap;
}
--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #5
..oO(jj)
>Excellent. Thanks, Kailash! I wouldn't have known what to look for.
Not really. Eval is evil.

What about using an array instead?

Micha
Nov 27 '07 #6
Kailash Nadh wrote:
On Nov 27, 8:40 pm, jj <jjwalke...@gmail.comwrote:
>Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable: $node->field_screen_1_caption[0]['view']. The loop should run n times,

incrementing that number 1 so next variable is $node-
>>field_screen_2_caption[0]['view'].
Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;

}

Doesn't work obviously. Any suggestions for a PHP amateur.

Thanks,
jj

eval() - http://php.net/eval

for ($i = 1; $i <= $number_of_screens; $i++) {
eval("\$cap = \$node->field_screen_{$i}_caption[0][\"view\"];");
print $cap;
}

--
Kailash Nadh | http://kailashnadh.name
Ewww, eval really?

Variable Variables -
http://au.php.net/manual/en/language...s.variable.php
- is probably closer to what you want.

Example:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = '$node->field_screen_' . $i . '_caption[0][\'view\']';
print $$cap;
}

Should work! However I *strongly* suggest using an array.

- Michael
Nov 28 '07 #7
Michael wrote:
Kailash Nadh wrote:
>On Nov 27, 8:40 pm, jj <jjwalke...@gmail.comwrote:
>>Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable:
$node->field_screen_1_caption[0]['view']. The loop should run n times,

incrementing that number 1 so next variable is $node-

field_screen_2_caption[0]['view'].
Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;

}

Doesn't work obviously. Any suggestions for a PHP amateur.

Thanks,
jj

eval() - http://php.net/eval

for ($i = 1; $i <= $number_of_screens; $i++) {
eval("\$cap = \$node->field_screen_{$i}_caption[0][\"view\"];");
print $cap;
}

--
Kailash Nadh | http://kailashnadh.name

Ewww, eval really?

Variable Variables -
http://au.php.net/manual/en/language...s.variable.php
- is probably closer to what you want.

Example:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = '$node->field_screen_' . $i . '_caption[0][\'view\']';
print $$cap;
}

Should work! However I *strongly* suggest using an array.

- Michael
Agh, whoops what i meant was...

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = '$node->field_screen_' . $i . '_caption';
print $$cap[0]['view'];
}

- Michael
Nov 28 '07 #8
jj wrote:
Trying to understand the manual regarding variables to solve the
following.

I'm working with Drupal CMS and have a loop to retrieve values that
are normally fetched by using this variable: $node-
>field_screen_1_caption[0]['view']. The loop should run n times,
incrementing that number 1 so next variable is $node-
>field_screen_2_caption[0]['view'].

Here's what I'm trying in my loop to change that number in the
variable using $i:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = 'node->field_screen_'.$i.'_caption[0][\'view\']';
print $$cap;
}

Doesn't work obviously. Any suggestions for a PHP amateur.

Thanks,
jj
As you can see by the replies there are many ways to do this... yet
another:

for ($i = 1; $i <= $number_of_screens; $i++) {
$cap = "node->field_screen_{$i}_caption[0]['view']";
echo $$cap;
}

Norm
Nov 28 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1545
by: skeeterbug | last post by:
i have a program that will display a number of diffferent results depending on the situation. one example of the code looks like <?php if(isset($_SESSION)) { print 'You entered: ' ....
5
13359
by: Aamer Nazir | last post by:
Hi, I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows: set...
2
371
by: TF | last post by:
hi, i am using a ListBox control on a windows form using VB.Net. when i set the DataSource property of the control to an ArrayList it fires following events in this order: SelectedIndexChanged...
8
9686
by: David McDivitt | last post by:
I need to set tabs on java generated pages. Pages have four sections: header, sidebar, body, and footer. The sidebar and body change dynamically. The tab key must go to anchors, fields, and buttons...
6
4298
by: shaan.shaan | last post by:
Hi I am trying to set an env. variable by exporting in unix environment on DB2. I am using export var_name=ON, but it is not taking the value of the var_name. This thing is working fine when I...
1
6439
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
41
2824
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
909
by: nairjones | last post by:
Hi everyone help me iam having a problem passing a value to an sql variable thru VBA. I have a stored procedure declare @rec delete tablename where field1 = @rec the variable is to get a...
4
2361
by: akshay01 | last post by:
Hi All, I am using the following code in which i am creating some textboxes and and as the for loop continues the name of the textboxes will also be unique for all the textboxes. now i want to...
0
7202
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
7460
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
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
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
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.