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
 
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
JonRimmer replied on at Permalink Reply
JonRimmer
Hi There
I wonder if you could point me in the right direction. I want do a similar thing. I want to wrap every 2 pages in two divs.
<div class=""><div class="grid-x grid-padding-x">
<?php if (is_object($thumbnail)) {
                          ?>
                          <div class="cell large-6 medium-6">
                            <a href="<?php echo h($url) ?>"
                               target="<?php echo h($target) ?>">
                              <?php
                              $img = Core::make('html/image', array($thumbnail));
                              $tag = $img->getTag();
                              $tag->addClass('img-responsive');
                              echo $tag; ?> </a>
                          <?php
                      } ?>
                      <?php if ($includeEntryText) {
                          ?>
                          <div class="single_project_text">

</div></div>

Hope you can help
Thanks
JonRimmer replied on at Permalink Reply
JonRimmer
I have managed to get it to wrap every 2 pages but for some reason it seems to be repeating the page list 6 times. Any help would be appreciated.

<?php
defined('C5_EXECUTE') or die("Access Denied.");
$c = Page::getCurrentPage();
/** @var \Concrete\Core\Utility\Service\Text $th */
$th = Core::make('helper/text');
/** @var \Concrete\Core\Localization\Service\Date $dh */
$dh = Core::make('helper/date');
if ($c->isEditMode() && $controller->isBlockEmpty()) {
    ?>
    <div class="ccm-edit-mode-disabled-item"><?php echo t('Empty Page List Block.') ?></div>
    <?php
} else {
    ?>
    <div class="ccm-block-page-list-wrapper">
        <?php if (isset($pageListTitle) && $pageListTitle) {