List split
2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 17, 2015 (RSS)
By master - November 8, 2015
I have a template that requires to do this
<div class="dates-wrapper">
<ul>
<li> <!-- List #1 --> <!-- max 5 Date Info elements per li -->
<div class="date-box"> <!-- Date Info 1 -->
</div> <!-- end Date Info 1 -->
<div class="date-box"> <!-- Date Info 2 -->
</div> <!-- end Date Info 2 -->
<div class="date-box"> <!-- Date Info 3 -->
</div> <!-- end Date Info 3 -->
<div class="date-box"> <!-- Date Info 4 -->
</div> <!-- end Date Info 4 -->
<div class="date-box"> <!-- Date Info 5 -->
</div> <!-- end Date Info 5 -->
</li>
<li> <!-- List #2 --> <!-- max 5 Date Info elements per li -->
<div class="date-box"> <!-- Date Info 1 -->
</div> <!-- end Date Info 1 -->
<div class="date-box"> <!-- Date Info 2 -->
</div> <!-- end Date Info 2 -->
<div class="date-box"> <!-- Date Info 3 -->
</div> <!-- end Date Info 3 -->
<div class="date-box"> <!-- Date Info 4 -->
</div> <!-- end Date Info 4 -->
<div class="date-box"> <!-- Date Info 5 -->
</div> <!-- end Date Info 5 -->
</li>
<li> <!-- List #3 --> <!-- max 5 Date Info elements per li -->
<div class="date-box"> <!-- Date Info 1 -->
</div> <!-- end Date Info 1 -->
<div class="date-box"> <!-- Date Info 2 -->
</div> <!-- end Date Info 2 -->
<div class="date-box"> <!-- Date Info 3 -->
</div> <!-- end Date Info 3 -->
<div class="date-box"> <!-- Date Info 4 -->
</div> <!-- end Date Info 4 -->
<div class="date-box"> <!-- Date Info 5 -->
</div> <!-- end Date Info 5 -->
</li>
</ul>
</div>
How can I split 5 records in a <li></li> and automatically create a new <li></li> with five more records?
By Daryl - November 17, 2015
Hi master,
You can achieve that by using a counter and add closing and opening li tags every 5 counts if the current array is not the last.
For example:
<div class="dates-wrapper">
<ul>
<li>
<?php
// get last record num
$lastExampleRecord = end($exampleRecords);
$lastExampleRecordNum = $lastExampleRecord['num'];
$counter = 0;
?>
<?php foreach($exampleRecords as $record): ?>
<div class="date-box"><?php echo $record['num'] ?></div>
<?php
// close <li> tag and open a new one every 5 records if this is not the last record
if (++$counter >= 5 && $lastExampleRecordNum != $record['num']){
print '</li><li>';
$counter = 0; // reset counter
}
?>
<?php endforeach; ?>
</li>
</ul>
</div>
Hope this helps!
Cheers,
PHP Programmer - interactivetools.com