我不确定如何解释这一点,但基本上我有一个x变量等于数据库中的行数,每次等于4或更少,运行一段代码。
所以说我有5行,我想执行两次代码,因为x等于4一次然后等于1.相同如果我有6行(4 + 2)7行(4 + 3)或8行(4 + 4)。
然后,当我达到9行(4 + 4 + 1)时,它会执行三次代码。等...
我正在尝试运行的代码是一段HTML,其中包含更多PHP。如上所述,每当行数等于4或更少时,我想在其中创建一个带有所述代码的新<ul>
。
现在的代码:
<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
}
?>
答案 0 :(得分:2)
如果行数有限,我会将结果放入一个数组中,然后使用array_chunk()
将它们分成几部分。
$results = array();
while ($row = mysql_fetch_array($result)) {
$results[] = $row;
}
foreach (array_chunk($results, 4) as $chunk) {
echo '<ul>';
foreach ($chunk as $row) {
echo '<li>', /*....*/, '</li>';
}
echo '</ul>';
}
答案 1 :(得分:1)
将x除以4并将其四舍五入。例如4/4 = 1,5 / 4 = 2,9 / 4 = 3.然后,使用for循环。
var noOfTimes = //x/4 rounded up
for (var i = 0; i < noOfTimes; i++) {
//insert code here
}
答案 2 :(得分:0)
使用ceil
功能。
http://us3.php.net/manual/en/function.ceil.php
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < ceil($num_rows / 4); $i++) {
// do stuff
}