I want to create a PHP Loop so that it should read an array of names and print it in a specific pattern so that on every 4 records, it should close and reopen the new UL element. So in general it should be like below :
$names = array('Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5', 'Name 6', 'Name 7', 'Name 8', 'Name 9');
This is the array I have and I want to create a loop so it should print like this
<ul>
<li>Name 1</li>
<li>Name 2</li>
<li>Name 3</li>
<li>Name 4</li>
</ul>
<ul>
<li>Name 5</li>
<li>Name 6</li>
<li>Name 7</li>
<li>Name 8</li>
</ul>
<ul>
<li>Name 9</li>
</ul>
$count = 0;
foreach ($names as $key => $value) {
if ($count%4 == 0) {
print '<ul>';
}
print "<li>$value</li>";
if ($count%4 == 3) {
print '</ul>';
}
$count++;
}
if (count($names)%4 != 0) {
print '</ul>';
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments