php 如何限制while循环中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4006069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:43:09  来源:igfitidea点击:

How to limit items from while loop

phpwhile-loop

提问by Zhaf

This is my while loop from my project :

这是我的项目中的 while 循环:

<?php
   $select = "SELECT * FROM nk_showcase";
   $query = $db->rq($select);
   while ($user = $db->fetch($query)) {
?>

    <div class="index">
        <a href="details.php?id=<?php echo $user['id']; ?>"><img width="200" height="171" alt="<?php echo $user['title']; ?>" src="<?php echo $url; ?>/images/niagakit/<?php echo $user['thumb']; ?>"/></a>
        <h3><a href="<?php echo $url; ?>/"><?php echo $user['title']; ?></a></h3>
        <p><a href="<?php echo $url; ?>/"><?php echo $user['url']; ?></a></p>
    </div>

<?php } ?>

As you already know, this while loop will loop for all items they found in my database, so my quuestion is, how to limit this loop only for 10 items only from my database and how to rotate that items every refresh?

正如您已经知道的,这个 while 循环将循环他们在我的数据库中找到的所有项目,所以我的问题是,如何将这个循环限制为仅来自我的数据库的 10 个项目,以及如何在每次刷新时轮换这些项目?

回答by Pekka

In SQL:

在 SQL 中:

$select = "SELECT * FROM nk_showcase LIMIT 0,10";

or in PHP:

或在 PHP 中:

$counter = 0;
$max = 10;

 while (($user = $db->fetch($query)) and ($counter < $max))
  {
   ... // HTML code here....

   $counter++;
  }

As to the rotating, see @Fayden's answer.

至于旋转,请参阅@Fayden 的回答。

回答by Vincent Savard

Rotate as in random, or as the next 10 elements ?

像随机旋转,还是像接下来的 10 个元素一样旋转?

Most RDBMS allow you to order rows by random :

大多数 RDBMS 允许您随机排序行:

-- MySQL
SELECT * FROM nk_showcase ORDER BY RAND() LIMIT 10
-- PostgreSQL
SELECT * FROM nk_showcase ORDER BY RANDOM() LIMIT 10

Which would select 10 random rows every time you refresh the page

每次刷新页面时都会随机选择 10 行

If you want to show the next 10 elements, you would have to paginate the page (and use the LIMIT X OFFSET Y syntax)

如果要显示接下来的 10 个元素,则必须对页面进行分页(并使用 LIMIT X OFFSET Y 语法)

回答by Nervo Verdezoto

You have to change your query $select, try using LIMITto 10 if you just need the 10 first items or try also with OFFSETif you need to paginate the results.

您必须更改您的查询 $select,如果您只需要前 10 个项目,请尝试使用LIMIT为 10,或者如果您需要对结果进行分页,请尝试使用OFFSET

$select.=" OFFSET $start LIMIT $range;";

Then you need to control the $startand $rangevariables like:

然后你需要控制$start$range变量,如:

$size_page=10;
 if (!$page) {
             $start = 0;
             $page=1;
        }
        else {
            $start = ($page - 1) * $size_page;
        } 

You can notice that $range should be the same value of $size_page and just you need to calculate the $start value for each iteration taking into account the #pages

您可以注意到 $range 应该与 $size_page 的值相同,并且您只需要计算每次迭代的 $start 值,同时考虑 #pages