需要自动分页帮助

问题描述 投票:1回答:1

我有这个脚本:

<?php
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles")
{
    include("Articles.php");
}
?>

这允许我设置页面的自定义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

依此类推。即,上面脚本中的代码将如下所示:

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");
}

但是每次具有最新文章的页面被填充(每页包含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");
}
php
1个回答
0
投票

您可以做的是,为了动态创建此文件,请使用功能glob,该功能可以列出目录中的所有文件。

鉴于我将拥有这些文件:

  • Articles-1.php
  • Articles-2.php
  • Articles-3.php
  • ...

而且,在默认情况下,glob按字母顺序对路径名进行排序,这意味着我们将不得不通过glob反转生成的文件数组。

array_reverse

这应该给您动态包含的权利。

© www.soinside.com 2019 - 2024. All rights reserved.