|
Posted by Michael on 11/28/07 06:21
Kailash Nadh wrote:
> On Nov 27, 8:40 pm, jj <jjwalke...@gmail.com> 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
Navigation:
[Reply to this message]
|