【发布时间】:2018-02-18 08:22:57
【问题描述】:
我有一个在页面中返回结果的 php 搜索代码。我想做的是将每页的结果数限制为 10,并回显链接以获得更多结果。
我的代码是:
$dir = 'www/posts';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
if (!empty($q)) {
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
$last_dot_index = strrpos($file, ".");
$withoutExt = substr($file, 0, $last_dot_index);
$fpath = 'posts/'.$file;
if (file_exists($fpath)) {
echo "<a href='/search.php?post=$withoutExt'>$withoutExt</a>" . " on " . date ("d F Y ", filemtime($fpath)) ;
echo "<br>";
}
}
}
closedir($res);
}
else {
echo "";
}
我尝试了 $q->limit(10);但它不起作用。请帮我写一个工作代码。
【问题讨论】:
-
您的搜索逻辑在
search.php文件中。 -
无论如何,您的代码中的空格、cmets 和漂亮的缩进太乱了,我们无法轻松阅读。
$q是一个 GET 参数(字符串),你对$q->limit(10)有什么期望?你知道你在这里做什么吗? -
检查
isset()然后empty()是多余的。不要这样做。 -
您的目标帖子是否有预期的后缀?他们是
.pdf吗?.txt,.html? -
是的。帖子文本是从 txt 文件中检索的
标签: php pagination filenames