【问题标题】:Need to split MySQL results into 3 per row需要将 MySQL 结果分成每行 3 个
【发布时间】:2012-11-29 09:21:54
【问题描述】:

我想将数据库中的结果分成每行 3 个结果。这是我的代码:

include("connect.php");

$result = mysql_query("SELECT * FROM movies ORDER BY title");
$num = mysql_num_rows ($result);

if ($num > 0 ) {
    $i=0;
    while ($i < $num) {
        $id = stripslashes(mysql_result($result,$i,"id"));
        $title = stripslashes(mysql_result($result,$i,"title"));
        $description = stripslashes(mysql_result($result,$i,"description"));
        $length = stripslashes(mysql_result($result,$i,"length"));
        $link = stripslashes(mysql_result($result,$i,"link"));
        $rating = stripslashes(mysql_result($result,$i,"rating"));
        $cover = stripslashes(mysql_result($result,$i,"icover"));

        $row .= '<tr><td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td><td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td><td><a href="update.php?id='.$id.'">Update</a></td><td><a href="delete.php?id='.$id.'">Delete</a></td></tr>';

        ++$i; 
    }
} 
else { 
    $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; 
}

mysql_close();

【问题讨论】:

  • 您能否举一个查询结果的示例以及您希望它的格式?您的消息来源并不太清楚您要做什么。
  • 模数运算符是你的朋友。
  • 请不要将 mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,这里是good PDO tutorial
  • @JohnConde 它们现在实际上已被正式弃用。 Here's更新的不要使用ext mysql评论
  • @Nick Fury,感谢您的更新!

标签: php mysql limit rows


【解决方案1】:

正如有人指出的那样,如果您想每 N 次执行一次,您通常需要检查表单:

if ( $i > 0 && $i % $N == 0) {
    // Do your Nth something here
}

其中 $i 是您当前的迭代次数,当然 $N 是您想要中断的频率。所以在这种情况下,$N = 3,你的中断逻辑将进入 if 语句的主体。

话虽如此,看起来可能涉及的不仅仅是这些;您的代码已经在您的表中进行了很多操作,因为您每行已经有多个列。您真的想要 3 组这些多列,还是您的意思是别的,比如行分组?

【讨论】:

    【解决方案2】:

    我认为您忘记在 if 语句上定义 $row ,在这种情况下,您的变量将仅是 while 内的局部变量

    include("connect.php");
    
     $result = mysql_query("SELECT * FROM movies ORDER BY title");
     $num = mysql_num_rows ($result);
     $row = '';
    
     if ($num > 0 ) {
        $i=0;
    
    
       while ($i < $num) {
    
       $id = stripslashes(mysql_result($result,$i,"id"));
       $title = stripslashes(mysql_result($result,$i,"title"));
       $description = stripslashes(mysql_result($result,$i,"description"));
       $length = stripslashes(mysql_result($result,$i,"length"));
       $link = stripslashes(mysql_result($result,$i,"link"));
       $rating = stripslashes(mysql_result($result,$i,"rating"));
       $cover = stripslashes(mysql_result($result,$i,"icover"));
    
       $row .= '<tr>
       <td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td>
       <td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td>
       <td><a href="update.php?id='.$id.'">Update</a></td>
       <td><a href="delete.php?id='.$id.'">Delete</a></td>
      </tr>';
    
      ++$i; }
     } else { $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; }
    
      mysql_close();
    
      print($row);
    

    【讨论】:

      【解决方案3】:

      我认为这可能会帮助您完成您正在尝试做的事情。当然,您需要对表格进行样式设置以满足您的需求。您可以将代码中的列数设置为适合您的任何值。

      $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
              if ($mysqli->connect_errno)
              {
                  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                  $mysqli->close();
              }
      
          $values = $mysqli->query("SELECT * FROM movies ORDER BY title");
          $cols = 3;
          $i =1;
          echo "<table>".PHP_EOL;
          echo "<tr>".PHP_EOL;
      while($row = $values->fetch_assoc())
          { 
              $id             = $row['id'];
              $title          = $row['title'];
              $description    = $row['description'];
              $length         = $row['length'];
              $link           = $row['link'];
              $rating         = $row['rating'];
              $cover          = $row['icover'];
          if (is_int($i / $cols))
              {
                  echo "<td >".PHP_EOL;
                  //what ever you want to include goes here
                  echo "</td>".PHP_EOL;
                  echo "</tr>".PHP_EOL;
                  echo "<tr>".PHP_EOL;
              }
              else
                  {
                      echo "<td >".PHP_EOL;
                      //what ever you want to include goes here
                      echo "</td>".PHP_EOL;
                  }
                  $i++;
          }
              echo "</tr>".PHP_EOL; 
              echo "</table>".PHP_EOL;
      

      【讨论】:

        猜你喜欢
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多