How to wrap page list code in DIV's every 4 items? PHP Question

Permalink 1 user found helpful
More of a PHP question I suppose but before I hop over to stackoverflow...

I'm hacking the Thumbview Template add-on view.php.

At the moment, the markup it generates is like this:
<div class="coda-slider">
   <div class="panel-wrapper">
      <div class="content_block">
         <a href=""><img class="ccm-output-thumbnail" alt="" src="" width="100" height="67" /></a>
         <h2><a href="">Thing</a></h2>
         <p></p>
      </div>
   </div>
        <div class="panel-wrapper">
      <div class="content_block">
         <a href=""><img class="ccm-output-thumbnail" alt="" src="" width="100" height="67" /></a>
         <h2><a href="">Thing</a></h2>
         <p></p>
      </div>
   </div>


However, I want to wrap every four <div class="panel-wrapper"> div's inside another div called '<div class="panel">'

How can I amend my view.php to achieve that? Copy paste example great but an explanation of what is done would be even better!

<?php
   if (count($cArray) > 0) { ?>
                  <div class="coda-slider">
   <?php 
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $title = $cobj->getCollectionName();
      $link = $nh->getLinkToCollection($cobj); ?>   
                     <div class="panel-wrapper">
                        <div class="content_block">
                        <?php 
                        $pt = $cobj->getCollectionAttributeValue('page_thumbnail');
                        if($pt){
                           echo("<a href=\"$link\">");
                           $imgHelper->outputThumbnail($pt, 100,100, $title);


Thanks for any help...

benfrainuk
View Replies: View Best Answer
glockops replied on at Permalink Best Answer Reply
glockops
You'll want to use the PHP mod function (%), but it's trickier than that, because you also need to start the wrapper for the first page and close it for the last (even if it only has 1 or 2 total pages in it).

Now tested code, so this *should* work.
<?php 
for ($i = 0; $i < count($cArray); $i++ ) {
   $cobj = $cArray[$i]; 
   $title = $cobj->getCollectionName();
   $link = $nh->getLinkToCollection($cobj); 
   $pt = $cobj->getCollectionAttributeValue('page_thumbnail');
   // Output a div around the wrapper for every 4th page
   if(!($i % 4)) {
      echo '<div class="panel">';
   }
   ?>   
    <div class="panel-wrapper">
        <div class="content_block">
        <?php 
        if($pt){


Further explanation
<?php
// Output a div around the wrapper for every 4th page
   if(!($i % 4)) {
      echo '<div class="panel">';
   }
   ?>

Here we are going to display the panel div if this is the first loop through the forloop or if the page number is evenly divisible by 4 (as in 4/4 has no remainder and gets the div, where 5/4 has a remainder of 1 and doesn't get the div) On the first loop this is asking what is the remainder of 0/4, the answer is 0 (which means false) So the ! in front of it says if this statement is NOT true (if mod says 0) then display the div.

<?php // Output ending div for every forth page
   if(!(($i+1)%4) || $i+1 == count($cArray)) {
      echo '</div><!-- End panel -->';
   }
   ?>

Here we're closing the div if this page is the fourth in a series ($i+1 - again we're counting from zero, where $i= 3 is the fourth page). Also we're checking to see if this is the last item being displayed. So if you're display a total of 10 items, you'll have two panels with 4 pages each and one panel with 2.

Hope the helps - and works :P
glockops replied on at Permalink Reply
glockops
Fixed previous code typos.
benfrainuk replied on at Permalink Reply
benfrainuk
Hi glockops, thanks very much for taking the time here to explain that. I'm going to give it a whirl shortly but more importantly go through your explanation in the hope I can understand it for next time.

Thanks again, Ben
benfrainuk replied on at Permalink Reply
benfrainuk
Just done a quick test and works perfect. Will have a closer look this evening to see how it's doing it's thing!

Thanks again - Ben