0

我不完全确定如何解释这一点,但基本上我有一个 x 变量等于数据库中的行数,每次它等于或小于 4 时,运行一段代码。

假设我有 5 行,我想执行两次代码,因为 x 等于 4 一次然后等于 1。如果我有 6 行 (4+2) 7 行 (4+3) 或 8 行 (4 +4)。

然后,当我达到 9 行 (4+4+1) 时,它将执行三次代码。ETC...

我试图运行的代码是一段 HTML,里面有更多的 PHP。<ul>如上所述,每次行数等于或小于 4 时,我想在其中创建一个新的带有上述代码的代码。

现在的代码:

<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
  }
  ?>

4

3 回答 3

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>';
}
于 2013-01-29T03:06:49.617 回答
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
}
于 2013-01-29T03:10:08.650 回答
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
}
于 2013-01-29T03:25:17.977 回答