Frage

Ive got the follow PHP:

<div class="slide-background">

       <div class="slide">
          <?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
          <?php if (12 / $cols == 1):?>
          <div class="col-md-12">
             <?php else: ?>
             <div class="col-md-<?php echo 12 / $cols; ?>">
                <?php endif; ?>
                <ul>
                   <?php foreach($items as $submenu): ?>
                   <?php echo $submenu; ?>
                   <?php endforeach; ?>
                </ul>
             </div>
             <?php endforeach; ?>
          </div>
          <ul class="pager">
             <li>prev</li>
             <li>next</li>
          </ul>
       </div>
    </div>

basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..

Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..

Any Help Greatly Appreciated..

UPDATE:

thanks to Hans ive now have the following:

<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">

       <div class="slide">
          <div class="col-md-<?php echo 12 / $cols ?>">
             <ul>
                <?php foreach ($items->submenu as $link): ?>
                <?php $linkCounter++;?>
                <?php if($linkCounter % $linksPerColumn == 0):?>
             </ul>
          </div>
          <div class="col-md-<?php echo  12 / $cols ?>">
             <ul>
                <?php endif; ?>
                <?php echo $link; ?>
                <?php endforeach; ?>
             </ul>
          </div>
       </div>
       <ul class="pager">
          <li>prev</li>
          <li>next</li>
       </ul>
    </div>
    </div>

only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...

War es hilfreich?

Lösung

You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:

<?    
$linksPerColumn = 4;
if ($linkCount > 4){
   $linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
   <div class="slide">
<?
   foreach ($links as $link)
   {
     $linkCounter++;
?>
// Do your HTML Here. 
<?
   if($linkCounter % $linksPerColumn = 0)
   {
?>
   </div>
   <div class="slide">
<?
   }
?>
   </div>
</div>
// Rest of the HTML here.

I think this should do the trick for you.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top