Ok so I thought i would put this one out there. I have a list of 25 entries in my db which I would like to display on my page, in list form, in 5 columns with 5 rows.
I prepared a solution, albeit not such an elegant one.
<?php
$query = "SELECT * FROM city ORDER BY name ASC";
$result = mysql_query($query, $connection);
confirm_query($result);
echo "<ul class=\"floatleft\">";
$counter = 1;
while ($row = mysql_fetch_array($result)) {
if ($counter <= 5) {
echo "<li>{$row["name"]}</li>";
$counter = $counter + 1;
if ($counter == 6) {
echo "</ul>";
}
} elseif($counter > 5 && $counter <= 10) {
if ($counter == 6){
echo "<ul class=\"floatleft\">";
}
echo "<li>{$row["name"]}</li>";
$counter += 1;
if ($counter == 11) {
echo "</ul>";
}
} elseif($counter > 10 && $counter <= 15) {
if ($counter == 11){
echo "<ul class=\"floatleft\">";
}
echo "<li>{$row["name"]}</li>";
$counter += 1;
if ($counter == 16) {
echo "</ul>";
}
} elseif($counter > 15 && $counter <= 20) {
if($counter == 16){
echo "<ul class=\"floatleft\">";
}
echo "<li>{$row["name"]}</li>";
$counter += 1;
if ($counter == 21) {
echo "</ul>";
}
} elseif($counter > 20 && $counter <= 25) {
if($counter == 21){
echo "<ul class=\"floatleft\">";
}
echo "<li>{$row["name"]}</li>";
$counter += 1;
if ($counter == 26) {
echo "</ul>";
}
}
}
?>
This will Output the following
<ul class="floatleft">
<li>Belfast</li>
<li>Birmingham</li>
<li>Brighton</li>
<li>Bristol</li>
<li>Cambridge</li>
</ul>
<ul class="floatleft">
<li>Cardiff</li>
<li>Carlisle</li>
<li>Edinburgh</li>
<li>Glasgow</li>
<li>Hull</li>
</ul>
<ul class="floatleft">
<li>Lancaster</li>
<li>Leeds</li>
<li>Leicester</li>
<li>Liverpool</li>
<li>London</li>
</ul>
<ul class="floatleft">
<li>Manchester</li>
<li>Newcastle</li>
<li>Norwich</li>
<li>Nottingham</li>
<li>Oxford</li>
</ul>
<ul class="floatleft">
<li>Plymouth</li>
<li>Portsmouth</li>
<li>Southampton</li>
<li>Swansea</li>
<li>York</li>
</ul>
Does anybody know of a better way of doing this? I’m still new to php, and I kbnow there must be a better, neater way of doing this.
Something like this should work.