我有这个脚本:
<?php
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles")
{
include("Articles.php");
}
?>
Run Code Online (Sandbox Code Playgroud)
这允许我为页面设置自定义 URL。现在,当我需要这样的 URL 时就会出现问题:
/?articles | Page that contains newest articles which loads when user clicks "< Previous" button
/?articles&1 | Page that contains older articles which loads when user clicks "Next >" button
/?articles&2 | Page that contains older than older articles
/?articles&3 | Page that contains old articles
/?articles&4 | Page that contains old articles
/?articles&5 | Page that contains oldest articles
/?articles&6 | Page that contains oldest articles, also last page in the pagination
Run Code Online (Sandbox Code Playgroud)
等等。也就是说,上面脚本中的代码是这样的:
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-500.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&2")
{
include("Articles-499.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&3")
{
include("Articles-498.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&4")
{
include("Articles-497.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&5")
{
include("Articles-496.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&6")
{
include("Articles-495.php");
}
Run Code Online (Sandbox Code Playgroud)
但是每次包含最新文章的页面被填满(每页包含 10 篇文章)时,我都必须像这样更新脚本:
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-505.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-504.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-503.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-501.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
include("Articles-500.php");
}
Run Code Online (Sandbox Code Playgroud)
我还需要你告诉我如何在我的导航中实际使用它,id est:<A href="?">Next</A>和<A href="?">Previous</A>
为了动态创建它,您可以做的是使用glob可以列出目录中所有文件的函数。
因此,假设您拥有以下文件:
\n\n现在,如果您的文章确实如上命名(Articles-1.php而不是Articles-001.php),您将面临简单的字母排序不起作用的问题。
\n事实上,按照简单的字母顺序排序,Articles-99.php始终会早于Articles-505.php显示。
但是,希望 PHP 为我们提供真正简洁的排序,即natsort().
\n\n\n该函数实现了一种排序算法,该算法以人类的方式对字母数字字符串进行排序,同时维护键/值关联。这被描述为“自然排序”。该算法与常规计算机字符串排序算法(在 中使用)之间的差异示例可以在下面的示例
\nsort()中看到。
通过排序natsort()比我最初的想法更好usort,这个想法的所有功劳都归功于 d\xc4\xb1lo s\xc3\xbcr\xc3\xbcc\xc3\xbc\ 的答案。
尽管如此,我们仍然需要以相反的顺序进行排序,这可以使用SORT_NATURAL上的标志来提供rsort()。
\n\n\nSORT_NATURAL - 使用“自然排序”将项目作为字符串进行比较
\nnatsort()
来源: https: //www.php.net/manual/en/function.sort.php
\n此标志的使用归功于Seh Horng\ 的回答
// Listing all the articles pages\n$articles = glob("Articles-*.php");\n// Applying reverse natural sorting, with SORT_NATURAL flag on rsort\nrsort($articles, SORT_NATURAL);\n\n/**\n * Extracting the selected page out of the REQUEST_URI, \n * and transform it an integer\n */\n$selectedPage = intval(\n str_replace("/?articles&", "", urldecode($_SERVER[\'REQUEST_URI\']))\n);\n\n/**\n * This is a sanity check; \n * just in case someone tries to access a page that does not exists,\n * like the page 99999999999\n * It also tackles the case when intval fails and returns 0\n * causing the index to be 0 - 1 = -1\n *\n * By the way, doing this check also covers your default behaviour\n * on the URL /?articles\n */\nif(array_key_exists($selectedPage - 1, $articles)){\n\n /** \n * We need to remove one from the selected page, \n * remembering an array start at 0 in PHP\n */\n include $articles[$selectedPage - 1];\n} else {\n include "Articles.php";\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这应该为您提供正确的动态包含。
\n