I’m not exactly sure how to explain this but basically I have an x variable equal to the number of rows in a database and each time it is equal to 4 or less, run a piece of code.
So say I have 5 rows, I want to execute the code twice because x is equal to 4 once and then equal to 1. Same if I had 6 rows (4+2) 7 rows (4+3) or 8 rows (4+4).
Then, when I reached 9 rows (4+4+1), it would execute the code thrice. Etc…
The code I’m trying to run is a piece of HTML with some more PHP inside it. I want to create a new <ul> with the said code inside it each time the number of rows is equal to 4 or less as explained above.
The code as it is right now:
<div class="row-fluid well">
<?php
$num_rows = mysql_num_rows($result);
if ($num_rows <= 4) {
?>
<ul class="minigames">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<li class='span3'>";
if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<a href='#!' class='thumbnail minigamedesc' title='" . $row['app_name'] . "' data-content='" . $row['app_about'] . "<br /><br /><center><a href=\"./games.php?play_error#library\" class=\"btn btn-info\"><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</a></center>'>";
} else if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "<a href='#!' class='thumbnail minigamedesc' title='" . $row['app_name'] . "' data-content='" . $app_about . "<a href=\"./games.php?play_error#library\">...</a><br /><br /><center><a href=\"./games.php?play_error#library\" class=\"btn btn-info\"><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</a></center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<a href='#!' class='thumbnail minigamedesc' title='" . $row['app_name'] . "' data-content='" . $row['app_about'] . "<br /><br /><center><a href=\"./play.php?app=" . $row['app_id'] . "\" class=\"btn btn-info\"><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</a></center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "<a href='#!' class='thumbnail minigamedesc' title='" . $row['app_name'] . "' data-content='" . $app_about . "<a href=\"./play.php?app=" . $row['app_id'] . "\">...</a><br /><br /><center><a href=\"./play.php?app=" . $row['app_id'] . "\" class=\"btn btn-info\"><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</a></center>'>";
} else {
echo "<a href='#!' class='thumbnail minigamedesc' title='" . $row['app_name'] . "' data-content='Nothing to see here, yet ! <br /><br /><center><a href=\"./play.php?app=" . $row['app_id'] . "\" class=\"btn btn-info\"><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</a></center>'>";
}
?>
<img src="<?php echo $row['app_preview']; ?>">
<div class="minigames-caption">
<h4><?php echo $row['app_name']; ?></h4>
<p>By <?php echo $row['app_dev']; ?>.</p>
</div>
</a>
<?php
echo "</li>";
}
?>
<?php
}
?>
If the number of rows are limited, I would put the results into one array and then use
array_chunk()to split them into portions.