469,356 Members | 2,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 1305
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by skeeterbug | last post: by
8 posts views Thread by David McDivitt | last post: by
6 posts views Thread by shaan.shaan | last post: by
41 posts views Thread by Jim | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.