1

This is my code

function THEME_preprocess_views_view_fields(&$variables)
{
    static $i = 0;
    $variables['address'] = t($variables['view']->result[$i]->field_address[0]['raw']['uri']);
    $variables['address1'] = t($variables['view']->result[0]->field_address[0]['raw']['uri']);
    $i++;
}

I wrote the following code in views-view-fields-VIEWNAME--page.tpl.php :

<?php print $address; ?>
<?php print $address1; ?>

the second print successfully printed its relevant value but the first one displayed nothing.

I tried to use this trick but i failed :

<?php
if($i==0)
    $variables['address'] = t($variables['view']->result[0]->field_address[0]['raw']['uri'];
if($i==1)
    $variables['address'] = t($variables['view']->result[1]->field_address[0]['raw']['uri'];
.
.
.

?>

why can't I use static variable as index?


UPDATE

after useful comment of Clive and arpitr I modified my code and I created two THEME_preprocess_views_view_fields functions separately as follow:

function THEME_preprocess_views_view_fields(&$variables)
{
    static $i = 0;
    $view = $variables['view'];
    if($view->name == "MY_VIEW_NAME")
    {
    $variables['address'] = t($variables['view']->result[$i]->field_address[0]['raw']['uri']);
    $i++;
     }
}

and this one

function THEME_preprocess_views_view_fields(&$variables)
{
    static $i = 0;
    $view = $variables['view'];
    if($view->name == "MY_VIEW_NAME")
    {
    $variables['address'] = t($variables['view']->result[$i]->field_address[0]['raw']['uri']);
     }
}

for the upper one it was the result

Array

and the second one (without $i++) successfully printed the right value.

7
  • 1
    do a dpm($i) inside THEME_preprocess_views_view_fields() and check the value it shows, I doubt the variable count is exceeding the value you are expecting result for.
    – arpitr
    Commented Oct 7, 2013 at 10:02
  • @arpitr Thanks for your suggestion. I did this, there are three elements but the result was 0 1 2 ... 15 16. so I modified the code and I updated my post. you can see it. thanks again
    – user21616
    Commented Oct 7, 2013 at 13:11
  • got you :), you should rather add this as answer.
    – arpitr
    Commented Oct 7, 2013 at 14:17
  • @arpitr it is very helpful but problem did not solve yet and I must complete this project as soon as possible :(
    – user21616
    Commented Oct 7, 2013 at 14:44
  • The static variables work fine for me. I just want to make sure you are using the correct spelling of properties here: usually, the field name in the $result array is referenced as field_<field_machine_name>, which, in your case would be field_field_address an NOT field_address.
    – pamatt
    Commented Oct 8, 2013 at 11:12

1 Answer 1

1

Static vars aren't special beyond persisting inside the scope of the function they were created, for the duration of a single page build. You categorically can use a static var as an array index.

Take the following code:

function some_test(&$variables) {
  static $i = 0;
  print $variables[$i];
  $i++;
}

$variables = array(
  0 => 'test1',
  1 => 'test2',
);

some_test($variables);
some_test($variables);

Which outputs the following:

test1
test2

Proving that $variables[$i] is accessed as expected.

The problem with you code must be somewhere else, as it's not being caused by a language issue as you suggest.

At a wild guess, $variables['view']->result isn't keyed in the same way you expect it to be (possibly keyed by the result's base table ID instead of a zero-based list). Dumping out $variables['view']->result should tell you pretty quickly what's happening.

2
  • I already dumped $variables['view']->result. I updated my post and showed the results and sounds like the problem is not by being static
    – user21616
    Commented Oct 7, 2013 at 13:14
  • I'm not sure what else I can tell you I'm afraid - your problem 100%, definitely isn't being caused by the use of a static var. It's completely illogical, unless you have a patched version of the PHP language which treats static vars differently (highly unlikely that you'd have that without knowing about it). The snippet in the answer above proves the point nice and succinctly - the problem you're having is caused by something else. If I had to guess, I would assume that $variables['view']->result[1] doesn't exist
    – Clive
    Commented Oct 7, 2013 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.