I'd like to alternate between two div tags in my node.tpl.php file so I can shade every other content item. The problem is, any counter I set in there, global or not, ends up getting reset for each iteration.

My code in node.tpl.php, for example:

if (is_odd($page_style_counter)) {

} else {
<? } $page_style_counter++; ?>

(if_odd is defined elsewhere)

any ideas?

Comments

dman’s picture

Get your scoping right.

global $page_style_counter;

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

BilldaCat’s picture

In the original post, I mentioned 'global or not', it didn't work. I tried setting it in node.tpl.php, as well as page.tpl.php, with no success.

dman’s picture

This worked exactly as expected for me:
node.tpl.php

global $page_style_counter; 
if(! $page_style_counter) $page_style_counter = 1;
$style = ($page_style_counter % 2) ? 'odd' : 'even';
print "Node number: $page_style_counter is $style";
$page_style_counter ++;

Drupal 5. PHP5.1.2. (zen theme)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

BilldaCat’s picture

don't know what i was doing wrong with the global declaration. appreciate the help.

dungkal’s picture

The snippet worked for me. Thanks.