0

I'm trying to modify search-result.tpl.php and template.php to color the backgrounds of search results returned such that every row has a different color than the one that preceded it. Only two colors are used. so, row #1 returned would have background #eee and row 2 would have #ff0000 as its background color. It would start over again at row #3 having #eee and so on. This is easily enough accomplished with PHP. But how can I implement this with Drupal?

Here's how I'm trying to print it out in search-result.tpl.php:

<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <div class="search-snippet-info">
    <?php $inc=0; ?>
      <?php if ($inc %2 == 0) {
                $color = '#eee';
              } else {  
                $color = '#ff0000';
              }
              ?>
      <div style="background-color: <?php print $color ?>">
      <div class="icon" style="float:left;"><?php print render($teaser['field_icon']); ?></div>
      <div class="teaser">
      <header>
      <h2><a href="<?php print $result['link']; ?>"><?php print $result['title']; ?></a></h2>
      </header>
      <em><?php print render($teaser['field_type']); ?></em>
      <strong><?php print render($teaser['field_event_dates']); ?></strong>
      <?php print render($teaser['body']); ?>
      </div>
      <?php $inc++; ?>
      </div>
  </div>
</li>

Not getting errors, but the results are not what I was expecting. Each row is just returning the same color background #eee, the first color). So either the $inc variable is not getting incremented or (perhaps more likely) it's becoming '0' again after every returned result. So then I'm thinking maybe I need to add something to my preprocess_search_result function in my template.php file...but I don't know what to add there.

Any help would be greatly appreciated.

1

In your search-result.tpl.php you should have a $zebra variable available for your use, which will be either "odd" or "even" so you can use that in place of trying to track your $inc variable. The $zebra variable is added by template_preprocess().

| improve this answer | |
1

I would create your search results page using views, this has the odd/even class functionality built-in. The current code you provided won't work since the $inc value starts as 0 and there is no loop where $inc++ occurs so everytime this template is loaded $inc will start as 0 and end as 1.

| improve this answer | |
  • thank you but I don't wish to use views for my search results; I've already done a bunch of customization in template.php and search-result.tpl.php and just have this last thing to finish up. Would you be able to recommend an option for making my code work (perhaps using template.php preprocess function to load the vars in advance and increment them somehow...?) – Sage Feb 16 '15 at 23:21
  • Have you tried views pre render? (drupal.org/node/438370)? – Fons Vandamme Feb 17 '15 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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