【问题标题】:PHP Limit number of results per pagePHP 限制每页的结果数
【发布时间】: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-&gt;limit(10) 有什么期望?你知道你在这里做什么吗?
  • 检查isset() 然后empty() 是多余的。不要这样做。
  • 您的目标帖子是否有预期的后缀?他们是.pdf 吗? .txt, .html?
  • 是的。帖子文本是从 txt 文件中检索的

标签: php pagination filenames


【解决方案1】:
<?php
$dir = 'www/posts';
$exclude = array(
    '.',
    '..',
    '.htaccess'
);
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : '';
$results=[];
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)) {
                $results[]="<a href='/search.php?post=$withoutExt'>$withoutExt</a>" . " on " . date("d F Y ", filemtime($fpath));
            }
        }
    }
    closedir($res);
}

foreach ($results as $result) {
    echo $result;
    echo "<br>";
}
if (count($results) > 10 ) {
    echo 'results is more than 10, but only 10 results is shown';
}

【讨论】:

  • 用你的代码它显示结果有超过 10 个,但它并不限制只有 10 个结果...仍然显示所有结果
  • 现在可以了。现在,如果我想回显“显示更多”链接以显示其余隐藏结果,我该如何实现?例如,现在如果总共有 15 个结果,它将只显示 10 个,但如果我想要一个显示更多链接以显示其余 5 个结果,我需要做什么?
【解决方案2】:

这是一个完全未经测试的建议...我很高兴来回折腾一下。

if(!empty($_GET['q'])){
    // perform some logical validation/sanitization on `$_GET['q']` for stability/security
    $path = '../posts'; // this relative path may need some adjusting
    $files=glob("$path/*.txt");  // collect all .txt files from the directory
    var_export($files);
    $filtered=preg_grep('~.*'.preg_quote($q,'/').'.*\.txt$~i',$files);  // case-insensitive filename search
    var_export($filtered);
    if(!empty($filtered)){
        $batches=array_chunk($filtered,10);  // store in groups of 10
        var_export($batches); // check the data
        if(!isset($_GET['page'],$batches[--$page])){  // a page has been submitted and that page exists in the batches array (subtract one to sync the page with the index)
            $page=0;
        }
        $leftovers=array_keys(array_diff_key($batches,[$page=>'']));
        foreach($batches[$page] as $filename){
            $withoutExt=substr($filename,0,strrpos($filename,"."));
            $fpath="posts/$filename";
            echo "<div><a href='/search.php?post=$withoutExt'>$withoutExt</a> on ",date("d F Y",filemtime($fpath)),"</div>";
        }
        // here you can list the other pages available from this search
        if($leftovers){
            echo "Additional pages:";
            foreach($leftovers as $offset){
                echo " <a href='/search.php?q={$_GET['q']}&page=",++$offset,"'>$offset</a>";
            }
        }
    }
}else {
    echo "No Search Terms found";
}

【讨论】:

  • 你能在这里给我看一下代码吗:pastebin.com/PtCpT9Es。唯一缺少的是代码的“显示更多”功能或 $leftovers 功能。
  • 不。我不想那样做。我故意使用glob() 和数组函数来完成这项工作。这一切都设置了“显示更多”组件。我很高兴在我的 sn-p 方面为您提供支持(我确信它不会按原样运行),但如果您愿意设置一些检查点,我可以协助使其正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多