Put a wrapper div around every second row in drupal view Hi there I am trying to use a views template override to put a wrapper div around every group of two rows. If the last group only has 1 row ( say if there were 5 rows total ) I want to end the wrapper div. 

I found some code for this but it's not working I am getting zero output from this I can see my comment tag but that it.

&lt;!-- test row output --&gt;

&lt;?php 
$group_nr = 1;                  // first group number
$last_row = count($rows) -1;    // last row
$wrapper  = 2;                  // put a wrapper around every 2 rows
?&gt;
&lt;?php foreach ($rows as $id =&gt; $row): ?&gt;
&lt;?php if ($id % $wrapper == 0) {print '&lt;div class="row"&gt;'; $i = 0; $group_nr++; } ?&gt;
&lt;?php print $row; ?&gt;
&lt;?php $i++; if ($i == $wrapper || $id == $last_row) print '&lt;/div&gt;'; ?&gt;
&lt;?php endforeach; ?&gt;


Any idea what I am doing wrong

My template is called views-view--homepage-awards--block-2.tpl.php

I want my output to look like this

&lt;div class="row"&gt;
  &lt;div&gt;row1&lt;/div&gt;
  &lt;div&gt;row2&lt;/div&gt;
&lt;/div&gt;
&lt;div class="row"&gt;
  &lt;div&gt;row3&lt;/div&gt;
  &lt;div&gt;row4&lt;/div&gt;
&lt;/div&gt;
&lt;div class="row"&gt;
 &lt;div&gt;row5&lt;/div&gt;
&lt;/div&gt;


Any help would be greatly appreciated.
This should accomplish it:

&lt;?php
  $last=count($rows) - 1;
?&gt;
&lt;?php foreach ($rows as $id =&gt; $row): ?&gt;
  &lt;?php if (!($id &amp; 1)): // if this an even row id, open the &lt;div&gt; ?&gt;
    &lt;div class="row"&gt;
  &lt;?php endif; ?&gt;
  &lt;div&lt;?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?&gt;&gt;
    &lt;?php print $row; ?&gt;
  &lt;/div&gt;
  &lt;?php if ($id==$last || ($id &amp; 1)): // if this the last or an odd row id, close the &lt;div&gt; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;
&lt;?php endforeach; ?&gt;


It'll wrap each group of two rows with a div with a class of row and make sure if it's an odd number that the final div is closed.

Now, if you want to remove all the classes from those inner rows that Views inserts, then you just need to clean them up or out of that obvious if in the middle.

CAVEAT:  If you have done anything with Views pre_render()ing or other similar hooks that have deleted any of the rows out of the results and not reset the ids to be consecutive, this logic will most probably fail.