Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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; 
$i++; 
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.

share|improve this question
    
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 May 14 '11 at 18:27
    
you put it in global php null –  Jeremy Love May 15 '11 at 19:05
add comment

3 Answers 3

up vote 2 down vote accepted

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++;
share|improve this answer
    
it worked thank you –  Jeremy Love Apr 20 '11 at 19:19
add comment

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
share|improve this answer
    
Thanks, Jason, this was just what I was looking for! –  uotonyh May 10 at 1:16
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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