1

I'm trying to display an ad every six rows of a view. I'm thinking of using another view that contains all the ads and displaying a random result of that every 6 rows of the main view.

Drupal 7 & Views 3.14

I have tried to do this but gotten stuck on the displaying something every 6 rows. Here is my code:

...code irrelevant, unedited coded copied from the template file in views module folder...
  <tbody>
    <?php foreach ($rows as $row_count => $row): ?>
      <tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?>>
        <?php foreach ($row as $field => $content): ?>
          <td <?php if ($field_classes[$field][$row_count]) { print 'class="'. $field_classes[$field][$row_count] . '" '; } ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
      </tr>
      <?php if ((($row_count % 6) == 0 && $row_count !== 0): ?><tr>This is the row after every 6th multiple</tr><?php endif; ?>
    <?php endforeach; ?>
  </tbody>
</table>

UPDATE: I figured out a way to print a random result. Just add randomize as the only sort in the view. However, I still can't get the printed view result to be in the appropriate location.

4
  • What happens when you run your code and how is it different to what you want to happen?
    – rooby
    Sep 25, 2016 at 23:07
  • Nothing happens... Sep 25, 2016 at 23:08
  • Looking at your code there, there is no $id variable, which is the one you are using to determine row number. What if you try $row_count?
    – rooby
    Sep 25, 2016 at 23:18
  • Yes, changing it to $row_count made a result. I updated the code. However, all of the <tr> tags are not showing up, but rather the results are just appearing at the very top of the table, not at the appropriate place. Sep 25, 2016 at 23:41

1 Answer 1

1

Okay, I solved it by using by using an unformatted list instead of a table.

Here is my code in the end:

<?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 
  $numRows = 6; /* Which row do you want the view to appear? E.g. 6 means that rows 6, 12, 18, etc. will have the view */
  if ((($id - ($numRows - 2) ) % ($numRows - 1)) == 0 && $id !== 0):
?>
    <div class="ads views-row"><?php print views_embed_view('VIEW_MACHINE_NAME', 'DISPLAY_ID',''); ?></div> 
  <?php endif; ?>
<?php endforeach; ?>

Your Answer

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

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