Views: Apply a wrapper to every 3 and 6 group of rows My Views Output is as follows

&lt;div class="views-row-1"&gt;Content&lt;/div&gt;
&lt;div class="views-row-2"&gt;Content&lt;/div&gt;
&lt;div class="views-row-3"&gt;Content&lt;/div&gt;
&lt;div class="views-row-4"&gt;Content&lt;/div&gt;
&lt;div class="views-row-5"&gt;Content&lt;/div&gt;
&lt;div class="views-row-6"&gt;Content&lt;/div&gt;
&lt;div class="views-row-7"&gt;Content&lt;/div&gt;
&lt;div class="views-row-8"&gt;Content&lt;/div&gt;
&lt;div class="views-row-9"&gt;Content&lt;/div&gt;
&lt;div class="views-row-10"&gt;Content&lt;/div&gt;
&lt;div class="views-row-11"&gt;Content&lt;/div&gt;
&lt;div class="views-row-12"&gt;Content&lt;/div&gt;
&lt;div class="views-row-13"&gt;Content&lt;/div&gt;
&lt;div class="views-row-14"&gt;Content&lt;/div&gt;
&lt;div class="views-row-15"&gt;Content&lt;/div&gt;
&lt;div class="views-row-16"&gt;Content&lt;/div&gt;
&lt;div class="views-row-17"&gt;Content&lt;/div&gt;
&lt;div class="views-row-18"&gt;Content&lt;/div&gt;


I would like to put a wrapper around every 3 and 6 group of Views rows.

So my mark up would look like this:

&lt;div class="group group-1"&gt;
&lt;div class="views-row-1"&gt;Content&lt;/div&gt;
&lt;div class="views-row-2"&gt;Content&lt;/div&gt;
&lt;div class="views-row-3"&gt;Content&lt;/div&gt;
&lt;/div&gt;

&lt;div class="group group-2"&gt;
&lt;div class="views-row-4"&gt;Content&lt;/div&gt;
&lt;div class="views-row-5"&gt;Content&lt;/div&gt;
&lt;div class="views-row-6"&gt;Content&lt;/div&gt;
&lt;div class="views-row-7"&gt;Content&lt;/div&gt;
&lt;div class="views-row-8"&gt;Content&lt;/div&gt;
&lt;div class="views-row-9"&gt;Content&lt;/div&gt;
&lt;/div&gt;

&lt;div class="group group-3"&gt;
&lt;div class="views-row-10"&gt;Content&lt;/div&gt;
&lt;div class="views-row-11"&gt;Content&lt;/div&gt;
&lt;div class="views-row-12"&gt;Content&lt;/div&gt;
&lt;/div&gt;

&lt;div class="group group-4"&gt;
&lt;div class="views-row-13"&gt;Content&lt;/div&gt;
&lt;div class="views-row-14"&gt;Content&lt;/div&gt;
&lt;div class="views-row-15"&gt;Content&lt;/div&gt;
&lt;div class="views-row-16"&gt;Content&lt;/div&gt;
&lt;div class="views-row-17"&gt;Content&lt;/div&gt;
&lt;div class="views-row-18"&gt;Content&lt;/div&gt;
&lt;/div&gt;


How can I do this? I know that I have to use views-view-unformatted.tpl.php but I am not sure how to alter the $row variable to target every 3rd and 6th group.

Thanks in advance for any help that can be offered.
A few tips about the code:


You also can make the logic in the preproccess or proccess hooks if you prefer
To complete the information given by Jack: Modular arithmetic . This theory help us to build our strategy.


Aplication in a preprocces hook to separate the logic from the template:

function YOURTHEME_preprocess_views_view_unformatted(&amp;$vars){

   $vars['prefix'] = array();
   $vars['suffix'] = array();
   $group = 1;
   $last_row = count($vars['rows']) -1;

   foreach($vars['rows'] as $id =&gt; &amp;$row){

     $vars['prefix'][$id] = '';
     $vars['suffix'][$id] = '';

     // Apply modular arithmetic
     $remainder = $id % 9;

     // First div =&gt; 3 items
     if($remainder == 0){
       $vars['prefix'][$id] = "\n &lt;div class=\"group group-$group\"&gt;";
     }

     if($remainder == 2){
       $vars['suffix'][$id] = '\n &lt;/div&gt;';
       $group++;
     }

     // Second item =&gt; 6 items
     if($remainder == 3){
       $vars['prefix'][$id] = "\n &lt;div class=\"group group-$group\"&gt;";
     }

     if($remainder == 8){
       $vars['suffix'][$id] = '\n &lt;/div&gt;';     
       $group++;
     }

     // Close the div in case there are not enough items
     if($last_row == id &amp;&amp; $remainder != 2 &amp;&amp; $remainder != 8){
        $vars['suffix'][$id] = '\n &lt;/div&gt;';
     }

   }

}


Your template in you theme that will overwrite the default template provided by views:

&lt;?php if (!empty($title)): ?&gt;
  &lt;h3&gt;&lt;?php print $title; ?&gt;&lt;/h3&gt;
&lt;?php endif; ?&gt;
&lt;?php foreach ($rows as $id =&gt; $row): ?&gt;
  &lt;?php if (!empty($prefix[$id])){ print $prefix[$id]; } ?&gt;
    &lt;div &lt;?php if ($classes_array[$id]) { print 'class="' . $classes_array[$id] .'"';  } ?&gt;&gt;
    &lt;?php print $row; ?&gt;
  &lt;?php if (!empty($suffix[$id])){ print $suffix[$id]; } ?&gt;
&lt;/div&gt;
&lt;?php endforeach; ?&gt;