简体   繁体   English

循环通过 MySQL 结果

[英]Looping through MySQL Results

I'm not sure exactly how this is called but I'll try to describe as good as I can what I want to acheive.我不确定这到底是怎么称呼的,但我会尽量描述我想要实现的目标。

So, first of all, there is a variable, called $id , which is actually $_GET['id'] .所以,首先,有一个变量,叫做$id ,它实际上是$_GET['id'] Assuming the user is entering the following page by requesting: /page.php?id=6 .假设用户通过请求进入以下页面: /page.php?id=6 Now what I need to do is to provide the information about the next 3 pages from database.现在我需要做的是从数据库中提供关于接下来 3 页的信息。 Here is the database:这是数据库:

  TABLE `pages`

  id   |   page_name
  ______________________
  1    |   AAAAA
  2    |   BBBBB
  3    |   CCCCC
  4    |   DDDDD
  5    |   EEEEE
  6    |   FFFFF
  7    |   GGGGG
  8    |   HHHHH
  9    |   IIIII

So, while requesting the page with id 6 , the following script returns the next 3 pages (7,8,9):因此,在请求 id 为6的页面时,以下脚本会返回接下来的 3 个页面 (7,8,9):

 $res = mysql_query("SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` DESC LIMIT 3");
 while($arr = mysql_fetch_assoc($res))
 {
       print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
 }

And here is the output:这是 output:

 Page ID: 7; Page Name: GGGGG
 Page ID: 8; Page Name: HHHHH
 Page ID: 9; Page Name: IIIII

And it works fine until the $id is greater then 6. When it is ( /page.php?id={7/8/9} ), the output doesn't show 3 pages any more, but 2 pages, 1 page and respectively no output when $id is 9 .它工作正常,直到$id大于 6。当它是( /page.php?id={7/8/9} )时,output 不再显示 3 页,而是 2 页,1 页当$id9时,分别没有 output 。

So my question is: Is there a way to go back and start from the beginning when there are not enough results (less than 3) to display?所以我的问题是:有没有办法让 go 在没有足够的结果(少于 3 个)显示时从头开始?

  • When accessing /page.php?id=8 , the output should contain pages with id 9 , 1 and 2 .当访问/page.php?id=8时,output 应该包含 id 为912的页面。
  • When accessing /page.php?id=9 , the output should contain pages with id 1 , 2 , 3 .当访问/page.php?id=9时,output 应该包含 id 为1 , 2 , 3的页面。
  • When accessing /page.php?id=3 , the output should contain pages with id 4 , 5 , 6 and so on.访问/page.php?id=3时,output 应包含 id 为456等的页面。
(SELECT *, 0 AS custom_order FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3)
UNION ALL
(SELECT *, 1 AS custom_order FROM `pages` ORDER BY `id` ASC LIMIT 3)
ORDER BY custom_order, id ASC
LIMIT 3

This way you always get 3 pages.这样你总能得到 3 页。 If not enough next pages, you will get up to 3 from the beginning.如果没有足够的下一页,您将从头开始最多 3 个。

You could modify the query to be something like:您可以将查询修改为:

 select * from 
     (select *, id-$inval($id) as order_by 
         from pages were id > $inval($id) order by id asc limit 3
      union
      select *, id as order_by 
         from pages order by id asc limit 3 ) as pages
 order by order_by asc

I would solve this way (one possible issue is that the resultset could contain at most 6 records instead of 3):我会这样解决(一个可能的问题是结果集最多可以包含 6 条记录而不是 3 条):

$res = mysql_query("(SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3) UNION DISTINCT (SELECT * FROM `pages` WHERE id>0 ORDER BY id ASC LIMIT 3)");
$counter = 0;
 while($arr = mysql_fetch_assoc($res) && $counter<3)
 {
       $counter++;
       print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM