PHP限制每页的结果数

时间:2018-02-18 08:22:58

标签: php pagination filenames

我有一个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-&gt;限制(10);但它不起作用。请帮我写一份工作代码。

2 个答案:

答案 0 :(得分:0)

<?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';
}

答案 1 :(得分:0)

这是一个完全没有经过考验的建议......我很高兴有一些反复来讨论它。

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