Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-MySQL“下一步”-“上一步”链接与订购_Php_Mysql_Navigation_Pagination_Fetch - Fatal编程技术网

PHP-MySQL“下一步”-“上一步”链接与订购

PHP-MySQL“下一步”-“上一步”链接与订购,php,mysql,navigation,pagination,fetch,Php,Mysql,Navigation,Pagination,Fetch,我有一张这样的桌子 id | name | image | ordering ------+-------------+------------+-------------- 1 | name 1 | one.jpg | 5 ------+-------------+------------+-------------- 2 | name 2 | two.jpg | 2 ------+----

我有一张这样的桌子

id    |   name      |  image     |    ordering
------+-------------+------------+--------------
1     |   name 1    |  one.jpg   |      5
------+-------------+------------+--------------
2     |   name 2    |  two.jpg   |      2
------+-------------+------------+--------------
3     |   name 3    |  thr.jpg   |      3
------+-------------+------------+--------------
4     |   name 4    |  for.jpg   |      7
------+-------------+------------+--------------
5     |   name 5    |  fiv.jpg   |      1
------+-------------+------------+--------------
6     |   name 6    |  six.jpg   |      9
------+-------------+------------+--------------
我的要求,以显示在一个页面上的第一个图像根据顺序。以下查询适用于我

SELECT * FROM images ORDER BY ordering ASC LIMIT 0,1 - row with id 5 will return
下一步,我必须在底部显示2个链接上一页和下一页,因为这是第一页不需要显示上一页

好的。。按下Next,我必须显示下一页,即根据表格显示id为2的行。在该页面中,需要显示将导致第一个结果的prev。该页的下一行必须指向id为3的行

我试过了

select * from images where id < $local_id order by id desc limit 1
 select * from images where id > $local_id order by id asc limit 1
但既然它有了订单,它就不起作用了

有谁能和我分享一个想法吗? 提前感谢

下一步

select * from images where id = $local_id+1 limit 1
先前的

select * from images where id = $local_id-1 limit 1
下一个

先前的

select * from images where id = $local_id-1 limit 1
在MySQL LIMIT X中,Y是您想要得到的范围,其中X是起始行0,是第一行,Y是要返回的行数

为了达到您想要的效果,您需要对每一页使用页码,然后使用页码计算X值,Y始终为1,因为您只需要在每一页上显示一幅图像

像这样的事情会让你开始:

<?php

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;

$startPoint = $page - 1;

$query = "SELECT * FROM images ORDER BY ordering ASC LIMIT $startPoint,1";

$rowCount = 0; // replace this with a count of all your rows/images
那么你会是这样的

<a href="index.php?page=1">First</a>
<a href="index.php?page=<?php echo $page - 1?>">Prev</a>
<a href="index.php?page=<?php echo $page + 1?>">Next</a>
<a href="index.php?page=<?php echo $rowCount;?>Last</a>
在MySQL LIMIT X中,Y是您想要得到的范围,其中X是起始行0,是第一行,Y是要返回的行数

为了达到您想要的效果,您需要对每一页使用页码,然后使用页码计算X值,Y始终为1,因为您只需要在每一页上显示一幅图像

像这样的事情会让你开始:

<?php

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;

$startPoint = $page - 1;

$query = "SELECT * FROM images ORDER BY ordering ASC LIMIT $startPoint,1";

$rowCount = 0; // replace this with a count of all your rows/images
那么你会是这样的

<a href="index.php?page=1">First</a>
<a href="index.php?page=<?php echo $page - 1?>">Prev</a>
<a href="index.php?page=<?php echo $page + 1?>">Next</a>
<a href="index.php?page=<?php echo $rowCount;?>Last</a>

您可以更改限制来实现它,根据页面的更改更改限制

    SELECT * FROM images ORDER BY ordering ASC LIMIT 1,1 - row with id 2 will return
    SELECT * FROM images ORDER BY ordering ASC LIMIT 2,1 - row with id 3 will return

您可以更改限制来实现它,根据页面的更改更改限制

    SELECT * FROM images ORDER BY ordering ASC LIMIT 1,1 - row with id 2 will return
    SELECT * FROM images ORDER BY ordering ASC LIMIT 2,1 - row with id 3 will return

不好+1/-1不好,阻止id页不存在,在上一个id所在的位置使用sql

示例ur主页id 3和id 4不存在。。。发生:

下一步:

以前的:

select * from images where id = (select max(id) from images where id < 4)

不好+1/-1不好,阻止id页不存在,在上一个id所在的位置使用sql

示例ur主页id 3和id 4不存在。。。发生:

下一步:

以前的:

select * from images where id = (select max(id) from images where id < 4)

这是一个非常巧妙和简单的解决方案。我是一个新手,所以除非我遗漏了什么,否则我喜欢它。这是一个非常巧妙和简单的解决方案。我是个新手,所以除非我错过了什么,否则我喜欢它。