如何限制while循环中的项目 [英] How to limit items from while loop

查看:24
本文介绍了如何限制while循环中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

<?php } ?>

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

解决方案

在 SQL 中:

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

或在 PHP 中:

$counter = 0;$max = 10;while (($user = $db->fetch($query)) and ($counter <$max)){...//这里的 HTML 代码....$计数器++;}

关于轮换,见@Fayden 的回答.

This is my while loop from my project :

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

解决方案

In SQL:

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

or in 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.

这篇关于如何限制while循环中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆