I'm using Views 2 on a sports website to pull up the 20 most recent article teasers for the front page. I'm trying to figure out a way to insert an add after every 5 articles returned.

The view style is unformatted, and I have successfully set up a php template to override the style so I can put in some custom code (as I couldn't find any setting in views to do this). However, I don't really know PHP, and I could really use a relevent tutorial or code snippet to help me out.

Can anyone give me a bit of advice as to how I can set up the PHP code that will allow me to count the nodes returned and after every certain number of nodes, insert my ad code? Or is there a way to do this within Views itself that I totally missed?

I used to use multiple views and throw the ad code in the footer of the view, with an offset applied to the number of articles returned for each subsequent view. I can't use this method anymore because I simply have too many ads to get in a page, and the number of views is starting to slow the site down.

Comments

dawehner’s picture

see for example this code

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
  <?php if ($id == 5) :?>
    yourcodecomeshere
  <?php endif; ?>
<?php endforeach; ?>
Geekish’s picture

Status: Fixed » Active

Thanks! That works perfectly. Following your code and taking what little I know about PHP operators, I can make the 'if' statement check for several numbers at once, like so:

if ($id == 5||$id == 10||$id == 15||$id == 20)

Now I can avoid stacking multiple offset views together and reduce the strain on my server resources. Thanks again, that really helped me out!

dawehner’s picture

Status: Active » Fixed

as you see learning php is worth to do it.

peterjmag’s picture

Status: Active » Fixed

Sorry to bump this back up, but for those who would like a method that can be scaled to a larger result set, try something like this:

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
* @file views-view-unformatted.tpl.php
* Default simple view template to display a list of rows.
*
* @ingroup views_templates
*/
?>
<?php
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
  <?php if (($id % 5) == 0): ?>
    yourcodecomeshere
  <?php endif; ?>
<?php endforeach; ?>
?>

You could also turn that into a function and use it elsewhere:

function is_multiple($integer, $multiple) {
  return ($integer % $multiple) == 0;
} 
Geekish’s picture

Actually, I'm very glad you posted. The other method works perfectly fine for a set number of articles on my front page and certainly got me out of a pinch. But then I started thinking about the archives and how it's not really feasible to keep specifying $ids in views where there are hundreds of articles that can be returned. And as I want a sprinkling of ads throughout the archive, I was trying to figure out (with my currently minimum PHP knowledge) how to do this very thing.

I may have been able to work it out eventually as I try to learn PHP, but you are a real time-saver! Thank you for posting this. It works like a charm for the views on my site that use pagers.

(However, I did want to mention that I think you might have an extra set of PHP tags surrounding the main block of code, as I had to remove a pair before the template would load without error.)

peterjmag’s picture

Glad I could help! And thanks for pointing out the extra tags--they were left over after I screwed up my formatting.

For future visitors, here's the corrected snippet:

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
* @file views-view-unformatted.tpl.php
* Default simple view template to display a list of rows.
*
* @ingroup views_templates
*/
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
  <?php if (($id % 5) == 0): ?>
    yourcodecomeshere
  <?php endif; ?>
<?php endforeach; ?>

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hectorplus’s picture

Subscribing

chrislabeard’s picture

You guys are awesome! You just saved my life!

arnaudfischer’s picture

subscribing

guvser’s picture

Status: Closed (fixed) » Active

How would the code look like in drupal 7?
I want to insert an adslot from the Google Admanager module (http://drupal.org/project/google_admanager) after every 10th result.

dawehner’s picture

Status: Active » Fixed

Really similar to this comment: http://drupal.org/node/536830#comment-1885334

You would have to insert this kind of code in the same template file

  <?php if (($id % 5) == 0): ?>
    yourcodecomeshere
  <?php endif; ?>

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jusyjim’s picture

Has anyone done this with a table view?

I tried using this code without luck:

<?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?>">
        <?php foreach ($row as $field => $content): ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
            <?php if (($id % 5) == 0): ?>
    contenthere
  <?php endif; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
jasom’s picture

Issue summary: View changes

This is my solution for custom code after n-th (first) row in Drupal 7 and Drupal 8 views.

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
  <?php if ($id == 0): ?>
your code goes here
  <?php endif; ?>
<?php endforeach; ?>

Note: In digi world data starts with zero: 0, 1, 2, 3... (not like we are used to 1, 2, 3...) so change "== 0" to "== 4" if you wanna to have your custom code after a 5th row.

Here is step-by-step tutorial for newbies how to achieve this: http://www.jasom.net/how-to-add-custom-code-adsense-after-first-row-in-d...