实现第一个、上一个、下一个和最后一个导航的方法

发布于 2024-08-24 12:10:06 字数 250 浏览 1 评论 0原文

我是编程新手,有一个关于导航的问题。 有哪些方法可以实现“第一个”、“上一个”、“下一个”和“最后一个”导航(如基于文章或漫画的网站中所示)? (诚​​然,“第一个”链接并不令人困惑,因为它可以保持静态,但其他链接呢?) 例如,在第 50 页上,链接将适当地指向第 49 页和第 51 页(如果存在,如果不存在,则不会起作用,但当此类页面存在时会自动变为活动状态。 在大多数示例中,我看到网址以“.php?id=50”结尾,但不确定它是如何通过数据库实现的? 任何帮助将非常感激,谢谢。

I'm new to programming and have a question about navigation.
What are the various methods that can achieve a "First", "Previous", "Next" and "Last" navigation, as seen in article or comic based sites? (Admittedly the "First" link isn't confusing as it can stay static, but what about the others?)
For example, on the 50th page, the links would appropriately lead to the 49th and 51st (if it exists, if not it would not function but would automatically become active when such a page exists.
In most examples of this I see urls ending with something like ".php?id=50" but am not certain how it's achieved, with a database?
Any help will be very much appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伴随着你 2024-08-31 12:10:06

URL 末尾的位称为 GET 变量。例如,当用户转到结尾为 1 的页面时:

example.org/comics/myawesomecomic.php?page=50

服务器转到该页面,并且(通常)服务器端脚本会在将该页面输出给用户之前执行一些工作。对于 PHP,GET 变量是一个全局变量(以及其他变量,其中最重要的是 POST 和 COOKIE),php 脚本可以通过获取从服务器传递到脚本的值来检索该变量,在本例中为 $_GET ['页面']。

因此,假设您有一个脚本来处理漫画故事情节“My Awesome Comic”的所有请求,并且它分为 60 个页面,则用户要访问任何页面所需要做的就是将 GET 变量更改为页码。您的脚本可能会执行一些非常简单的操作,例如获取名为 myawesomecomic/ 的目录的内容并获取名为 50.html 的文件,然后将其输出给用户。但很可能,自从遇到这个麻烦后,您想要一些更复杂的东西,因此它会从数据库中获取第 50 页并更新所有导航,甚至可能会抛出一个时尚的 50 - 60 在顶角。

现在,这是简单的部分,也就是您问题的答案。现在您已经知道 GET 变量如何工作以及服务器脚本如何处理它们,要使导航像您所描述的那样,您所要做的就是:

$current_page = $_GET['page'];
$next_page = $current_page + 1;
$prev_page = $current_page - 1;
$first_page = 1;
$last_page = 

最后一部分将由您来了解。要么您可以始终拥有相同的页面数,要么您可以在提取当前页面的同时查询数据库,等等。假设您在获取当前页面时确实在数据库中查询了总页数。它返回 60(我们假设,只是为了避免令人头疼的问题,60 是从 1 开始的页数,而不是 0),所以回到导航...

$last_page = 60;

现在您有了第一个、最后一个、上一个和下一个。假设您了解 HTML,那么剩下的就是(除了使其看起来漂亮之外):

 echo "
 <a href='myawesomecomic.php?page=$first_page'>First Page</a>
 <a href='myawesomecomic.php?page=$prev_page'>Previous Page</a>
 <a href='myawesomecomic.php?page=$next_page'>Next Page</a>
 <a href='myawesomecomic.php?page=$last_page'>Last Page</a>
 ";

还有您的导航。显然,您需要安排它们以适合您的网站,而不是让它们像那样运行在一起,使用很酷的箭头等。但是即使您不做任何花哨的事情,链接也会起作用。

但!

我刚刚给了您非常好的“我第一次享受 PHP 的乐趣”版本。我遗漏的事情(您应该询问更多信息或进行一些认真的研究):

  • 由于 GET 变量包含在 URL 中,因此它们并不难弄清楚。我可以轻松地将 ?page=50 更改为 ?page=1=1,并且根据您获取内容的方式,我可能只是要求提供您的所有内容数据库。 永远不要将用户输入(甚至是 URL)直接抓取到变量中。如果您知道它的页码,那么您就知道它是一个数字。在通过脚本和数据库运行输入之前,请检查输入以确保它是一个数字。

  • 您需要有一个系统来应对用户输入的数字超出范围的情况。一些简单的事情,比如“抱歉,找不到页面”,甚至“找不到页面,从头开始”,然后将它们发送回第 1 页,等等。如果您什么也不输出,那么看起来很糟糕,并且会暴露脚本逻辑中可以被操纵的部分。

  • 当您熟悉 GET 变量时,我建议放弃它们而使用 mod_rewrite(假设您使用的是 apache 服务器)。现在先不要跳入其中,但是当您掌握了窍门后,您可以开始创建如下 URL:example.org/comics/myawesomecomic/page50example.org/comics/myawesomecomic /page/50 或者你有什么。真正发生的情况是该目录中的所有页面都被重定向到同一个脚本,然后该脚本检查 URL 的结尾以找出要从数据库中提取的内容。但对于最终用户来说,看起来就像只是一个网页。这看起来更干净(我认为)并且可以更多地覆盖脚本的逻辑。但我说要等待,因为它还会给您带来其他风险,这些风险可能很难预测,直到您习惯清理用户输入并处理“找不到页面”等标准页面错误。

不管怎样,弄清楚在当前日期中添加一周以及在当前页面中添加页码之类的事情完全是我进入 PHP 和 Web 应用程序开发的方式,所以我希望这对您有所帮助且简单明了。

The bit at the end of the URL is called a GET variable. When a user goes to a page with one at the end, for example:

example.org/comics/myawesomecomic.php?page=50

The server goes to that page and (usually) a server-side script will do some work before outputting the page to the user. With PHP, the GET variable is a Global Variable (along with others, the big ones being POST and COOKIE), that the php script can retrieve by getting the value passed from the server to script, in this case in $_GET['page'].

So assuming you have a script that handles all requests for the comic storyline "My Awesome Comic" and it's broken up into 60 pages, all a user has to do to get to any page is change the GET variable to the page number. Your script might do something really simple, like get the contents of a directory called myawesomecomic/ and grab a file called 50.html and then output it to the user. But chances are, you want something a tad more sophisticated since you went to this trouble, so instead it goes and grabs page 50 from a database and updates all of the navigation and maybe even throws a stylish 50 - 60 in the top corner.

Now, here's the easy part, aka the answer to your question. Now that you know how GET variables work and what the server-script does with them, all you have to do to make navigation like you describe is:

$current_page = $_GET['page'];
$next_page = $current_page + 1;
$prev_page = $current_page - 1;
$first_page = 1;
$last_page = 

And that last part is going to be up to you to know already. Either you can always have the same number of pages, or you can query the DB at the same time you pull up the current page, etc. Let's say you did query the DB for the total page count while you were getting the current page. And it returned 60 (and we'll assume, just to avoid headaches that 60 is the page count starting from 1, not 0), so back to the navigation...

$last_page = 60;

Now you have your first, last, prev, and next. Assuming you know your way around HTML, all that's left (besides making this look pretty) is:

 echo "
 <a href='myawesomecomic.php?page=$first_page'>First Page</a>
 <a href='myawesomecomic.php?page=$prev_page'>Previous Page</a>
 <a href='myawesomecomic.php?page=$next_page'>Next Page</a>
 <a href='myawesomecomic.php?page=$last_page'>Last Page</a>
 ";

And there's your navigation. Obviously you'll want to arrange them to fit your site, not have them run together like that, use cool arrows, etc. But the links will work even if you don't do anything fancy.

BUT!

I just gave you the really nice, "my first time having fun with PHP" version. Things I left out (that you should ask more about or do some serious research on):

  • Since GET Variables are including on the URL, they aren't hard to figure out. I could easily change ?page=50 to ?page=1=1 and, depending on how you are getting the content, I may have just asked for all contents of your database. Never grab user-input (even the URL) straight into your variables. If you know it's page number, then you know it's a number. Check the input to make sure it's a number before running it through your script and your database.

  • You need to have a system for if the user puts in a number and it's out of range. Something simple like "Sorry, page not found" or even "Page not found, starting at the beginning" and sending them back to page 1, whatever. If you just output nothing, it looks bad and you reveal parts of your script's logic that could be manipulated.

  • When you get comfortable with GET variables, I suggest ditching them for mod_rewrite (assuming you are on an apache server). Don't jump in now, but when you get the hang of it, you can start making URLs like: example.org/comics/myawesomecomic/page50 or example.org/comics/myawesomecomic/page/50 or what have you. What really happens is all pages in that directory get redirected to the same script which then checks the URL's ending to figure out what to pull from the DB. But to the end-user, it looks like there's just a webpage. This looks cleaner (I think) and does even more to cover your script's logic. But I'm saying to wait on it because it also opens you up for other risks that can be harder to anticipate until you're used to sanitizing your user-input and handling standard page errors like "page not found."

Anyways, figuring out stuff like adding a week to a current date and adding a page number to the current page is totally how I got into PHP and web app development, so I hope this was helpful and straightforward.

新一帅帅 2024-08-31 12:10:06

这种导航称为分页。影响给定页面上分页的三个变量:

  • 项目总数
  • 每页的项目数
  • 当前页面

查询轻松地从数据库中获取项目总数:

$query = "SELECT COUNT(*) FROM tbl";
$count = mysql_result($query, 0);

您通常可以使用如下 每页的项目数和当前页数,您可以计算出总共有多少页:

$perpage = 10; // This you can define yourself

$pages_count  = ceil($count / $perpage);

之后,很容易做一些简单的 if 子句来查看应该显示什么样的导航:

// Get the current page or set default if not given
$page = isset($_GET['page']) ? $_GET['page'] : 1;

$pages_count = ceil($count / $perpage);

$is_first = $page == 1;
$is_last  = $page == $pages_count;

// Prev cannot be less than one
$prev = max(1, $page - 1);
// Next cannot be larger than $pages_count
$next = min($pages_count , $page + 1);

// If we are on page 2 or higher
if(!$is_first) {
    echo '<a href="index.php?page=1">First</a>';
    echo '<a href="index.php?page='.$prev.'">Previous</a>';
}

echo '<span>Page '.$page.' / '.$pages_count.'</span>';

// If we are not at the last page
if(!$is_last) {
    echo '<a href="index.php?page='.$next.'">Next</a>';
    echo '<a href="index.php?page='.$pages_count.'">Last</a>';
}

从数据库中选择正确的结果对于给定的页面,您可以使用如下查询:

$query = "SELECT * FROM tbl LIMIT ".(int)($page - 1)." ".(int)$perpage;

这就是它的全部内容。希望这有帮助。

This kind of navigation is called pagination. There are three variables that affect pagination on a given page:

  • The total number of items
  • The number of items per page
  • Current page

You usually can get the total number of items from a database easily, using a query like this:

$query = "SELECT COUNT(*) FROM tbl";
$count = mysql_result($query, 0);

And as you know the number of items per page and the current page, you can figure out how many pages are in total:

$perpage = 10; // This you can define yourself

$pages_count  = ceil($count / $perpage);

After that, it's easy to do some simple if clauses to see what kind of navigation should be displayed:

// Get the current page or set default if not given
$page = isset($_GET['page']) ? $_GET['page'] : 1;

$pages_count = ceil($count / $perpage);

$is_first = $page == 1;
$is_last  = $page == $pages_count;

// Prev cannot be less than one
$prev = max(1, $page - 1);
// Next cannot be larger than $pages_count
$next = min($pages_count , $page + 1);

// If we are on page 2 or higher
if(!$is_first) {
    echo '<a href="index.php?page=1">First</a>';
    echo '<a href="index.php?page='.$prev.'">Previous</a>';
}

echo '<span>Page '.$page.' / '.$pages_count.'</span>';

// If we are not at the last page
if(!$is_last) {
    echo '<a href="index.php?page='.$next.'">Next</a>';
    echo '<a href="index.php?page='.$pages_count.'">Last</a>';
}

To select the correct results from a database for the given page, you can use a query like this:

$query = "SELECT * FROM tbl LIMIT ".(int)($page - 1)." ".(int)$perpage;

That's really all there's into it. Hope this helps.

无人问我粥可暖 2024-08-31 12:10:06

也许本教程或< a href="http://www.phpfreaks.com/tutorial/basic-pagination" rel="nofollow noreferrer">这个会对您有所帮助。

编辑:嗯,第一个是一个非常烦人的网站。找到另一个。 :)

Perhaps this tutorial or this one will help you.

Edit: meh, the first one was a highly annoying site. Found another. :)

盛夏已如深秋| 2024-08-31 12:10:06

我假设您不是在谈论分页(即将大量记录列表拆分为多个页面),而是在谈论记录的详细视图,以及预先提供指向下一个和上一个记录的“下一个”/“上一个”链接。

如果你没有固定的方法来找出前一个和最后一个邻居(所以你不能只说“当前ID -1”和“+1”),你将不得不按照你的条件进行数据库查询,并找出上一个和下一个成员。

也许这是对问题的深入探讨,而不是您需要开始的问题,但我最近提出了一个问题,涉及优化问题和以快速有效的方式进行“邻居查找”的不同方法。 缓存“下一个”/“上一个”的结果元素”查询

I am assuming you are not talking about pagination (i.e. splitting a big list of records into multiple pages) but about having a detail view of a record, and preoviding "next" / "previous" links to the next and previous record.

If you have no fixed way of finding out the previous and last neighbours (so you can't just say "current ID -1" and "+1") you will have to do a database query ordered by your criteria, and find out the previous and next members.

Maybe it's to deeply into the issue, and not what you need to start out, but I asked a question recently that concerns optimization issues and differnt approaches to do this "neghbor lookup" in a fast and efficient way. Caching the results of a “next” / “previous element” query

魂归处 2024-08-31 12:10:06

有很多方法可以做到这一点,从复杂到简单。
(在谷歌上搜索 php 分页)。

某些 url 中的 ?id=50 是确定起始项目。

例如,在 sql 中,您可以使用 SELECT * FROM tbl LIMIT 0,10 确定要选择的项目范围,这将选择记录 0-10。那么 SELECT * FROM tbl LIMIT 11,10 将选择 11 到 20。

Theres quite a few methods to go about doing this, ranging from complex to simple.
(Seach for php pagination on google).

The ?id=50 in some urls is to determine the item to start at.

For example in sql, you determine a range of items to select using SELECT * FROM tbl LIMIT 0,10 , This will select records 0-10. then SELECT * FROM tbl LIMIT 11,10 would select 11 to 20.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文