Setting a varialble using a variable
Setting a varialble using a variable
am 27.11.2007 21:40:24 von 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
Re: Setting a varialble using a variable
am 27.11.2007 21:57:05 von Kailash Nadh
On Nov 27, 8:40 pm, 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
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
Re: Setting a varialble using a variable
am 27.11.2007 22:18:57 von 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;
>
> }
Re: Setting a varialble using a variable
am 27.11.2007 22:25:12 von luiheidsgoeroe
On Tue, 27 Nov 2007 21:40:24 +0100, 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 =3D 1; $i <=3D $number_of_screens; $i++) {
> $cap =3D '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 thou=
gh.
-- =
Rik Wasmus
Re: Setting a varialble using a variable
am 27.11.2007 22:53:06 von Kailash Nadh
On Nov 27, 9:18 pm, jj wrote:
> 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
Re: Setting a varialble using a variable
am 28.11.2007 00:22:26 von Michael Fesser
..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
Re: Setting a varialble using a variable
am 28.11.2007 07:21:23 von Michael
Kailash Nadh wrote:
> On Nov 27, 8:40 pm, 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
>
> 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.variables.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
Re: Setting a varialble using a variable
am 28.11.2007 07:23:45 von Michael
Michael wrote:
> Kailash Nadh wrote:
>> On Nov 27, 8:40 pm, 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
>>
>> 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.variables.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
Re: Setting a varialble using a variable
am 28.11.2007 14:10:38 von Norman Peelman
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