I’m just beginning with PHP and mySQL (about 4 days in now) and have been trying to find a fix to this problem…
I have a set of items that I am returning from a mySQL database. I have these flowing into a set of 4 “ul” columns with a class of “models” with it dividing the number of results by 4 and returning “li”s and cannot figure out why I have a phantom empty “ul” at the end. I have tried putting the “ul”s into a foreach statement but that didn’t return the desired results either.
Thanks for any help.
Here is the php:
<grid>
<ul class="models">
<?php
$c = count($model);
$s = ($c / 4); // change 4 to the number of columns you want to have.
$i=1;
foreach ($model as $models): ?>
<li>
<a href="model/?id=<?php htmlout($models['sn']); ?>" class="id<?php htmlout($models['sn']); ?>">
<div class="<?php htmlout($models['callout']); ?>"></div>
<div id="description">
<h2><?php htmlout($models['firstname']); ?> <?php htmlout($models['lastname']); ?>
</h2>
<h3><span class="number">40</span> photo sets</h3>
<h3><span class="number">756</span> images</h3>
<p><?php htmlout($models['rightstext_left']); ?>
<?php htmlout($models['firstname']); ?>
<?php htmlout($models['rightstext_right']); ?>
</p>
</div>
</a>
</li>
<?php if($i != 0 && $i % $s == 0)
{
?>
</ul>
<ul class="models">
<?php
}
$i++; ?>
<?php endforeach; ?>
</ul>
</grid>
The HTML
<grid>
**excluded**
<ul class="models">
<li>
<a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
<div class="exclusive"></div>
<div id="description">
<h2>Model 19</h2>
<h3><span class="number">1</span> attribute</h3>
<h3><span class="number">6</span> attributes</h3>
<p>textleft name textright</p>
</div>
</a>
</li>
<li>
<a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
<div class="exclusive"></div>
<div id="description">
<h2>Model 19</h2>
<h3><span class="number">1</span> attribute</h3>
<h3><span class="number">6</span> attributes</h3>
<p>textleft name textright</p>
</div>
</a>
</li>
<li>
<a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
<div class="exclusive"></div>
<div id="description">
<h2>Model 19</h2>
<h3><span class="number">1</span> attribute</h3>
<h3><span class="number">6</span> attributes</h3>
<p>textleft name textright</p>
</div>
</a>
</li>
<li>
<a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
<div class="exclusive"></div>
<div id="description">
<h2>Model 19</h2>
<h3><span class="number">1</span> attribute</h3>
<h3><span class="number">6</span> attributes</h3>
<p>textleft name textright</p>
</div>
</a>
</li>
<li>
<a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
<div class="exclusive"></div>
<div id="description">
<h2>Model 19</h2>
<h3><span class="number">1</span> attribute</h3>
<h3><span class="number">6</span> attributes</h3>
<p>textleft name textright</p>
</div>
</a>
</li>
</ul>
I changed your logic a little bit. Since you work with modulo, it would make more sense to start with
$i = 0You want to be sure to have an integer when you’re doing modulo operations, so try:
Then use it in your
ifto test if you’re at the end of a<ul>element, and make sure you don’t create one on the last iteration: