使用 glob 自动创建下一个和上一个类型页面
尝试使 glob 函数仅显示前 10 个结果(文件夹中的文本文件),然后通过“下一步”按钮自动显示后 10 个结果。
目前,我使用 array_slice 仅显示前 10 个,但我不确定如何自动化该过程。
foreach(array_slice(glob("*.txt"),0,9) as $filename) {
include($filename); }
哦,当我这样做时,如果可能的话,显示类似“显示 1-10”和最后一个/第一个链接的内容。在克服第一个障碍后,我可能会自己解决这个问题,所以不要为此烦恼,除非这是一个超级简单的解决方案。
Trying to make it so that the glob function displays only the first 10 results (text files from a folder) and then automatically the next 10 with a next button.
Currently, I used array_slice to display only the first 10, but I'm not sure how to automate the process.
foreach(array_slice(glob("*.txt"),0,9) as $filename) {
include($filename); }
Oh and while I'm at it, if possible, display something like "displaying 1-10" and last/first links. I could probably figure that out myself after I get past the first obstacle, so don't bother with this unless it's a super easy solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要完成的任务称为分页。在您的示例中,您可以通过设置一个确定从哪个数字(文件)开始的变量来动态选择您正在查看的十个。
示例:
如果您只想增量 10,则可以添加一个检查以确保
$start
能被 10 整除,在从$_GET
数组:现在,对于链接,您所需要做的就是输出正确设置了起始参数的
标记。您必须执行一些逻辑才能正确计算下一个和上一个值,但这里有一个简单的示例:
编辑: 正如上面的评论所暗示的,您可能不希望
include()
这些文件,因为include()
会尝试将这些文件解释为 PHP 脚本。如果您只需要获取文本文件的内容,请使用 file_get_contents 代替。What you're trying to accomplish is called pagination. From your example, you can dynamically choose which ten you're viewing by setting a variable that determines what number (file) to start at.
Example:
If you only want to do increments of 10, you can add a check to make sure
$start
is divisible by 10, something like this after$start
is retrieved from the$_GET
array:Now, for links, all you need to do is output
<a>
tags that have the start parameter set correctly. You will have to do some logic to calculate the next and previous values correctly, but here is a simple example:Edit: As the comments above suggest, you probably do not want to
include()
these files, asinclude()
will attempt to interpret these files as PHP scripts. If you just need to get the contents of the text files, use file_get_contents instead.