Im using Drupal 7, and I am trying to make a slide show with a numbered pager like the one shown in this page. I added PHP code in Views, thinking I could simple put PHP code, but the problem is that it is not producing the expected result.

I used this code, which is supposed to print "1 2 3 4", and that instead prints "1 1 1 1."

$i = 0; 
echo $i; 

In this code I tried using one of the variables, and I think I am on the right track with this one, but it prints "10 9 8 7," while I want it prints "1 2 3 4."

$i = 0; 
$i = $row->field_image; 
echo $i; 

I know that this is probably something simple, but I can't figure out how to save my life; any help is greatly appreciated.

  • I've been trying to do the same thing. Where do you put that code (default argument?) and i'm assuming that 'mycode_iterator_count' stands for something else. Any help is appreciated.
    – user1277
    Commented May 14, 2011 at 18:27
  • you put it in global php null Commented May 15, 2011 at 19:05

3 Answers 3


The first code you wrote doesn't work because, all times it is invoked

  • it initializes $i to 0
  • it increments $i by 1
  • it prints the value of $1

Because the first step, the result is always 1. To get a result that is different in any cases, the code should be similar to the following.

$i = variable_get('mycode_iterator_count', 0) + 1;  
echo $i;
variable_set('mycode_iterator_count', $i);

The code is not optimized, and probably there is a better way to write the code; although, the code works, as it will print a different number every time it is invoked.
As the code is passed to eval(), using a static variable would not probably serve to the purpose because the static variable will probably be reset to 0 every time the code is executed through eval(). Probably the code would work even if it would use a global variable.

global $i;

if (empty($i)) {
  $i = 1;

echo $i++;

Much easier way:

  1. Add "Global: View result counter" Field
  2. Exclude it from Display
  3. In Slideshow setting: choose that field as the Pager Type

Embedding PHP inside the view is a bad idea. PHP Views should be avoided. Instead I have come up with the following which works with default views:

  1. Click "add" on the fields box.
  2. Add a "Global: Math expression" field to your fields.
  3. In the "Expression" field enter [expression] + 1
  4. Click "Exclude from display".
  5. Set your pager field to be the expression.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.