php 分页结果 每页限制


php pagination result Limit per page

使用简单的 dom 解析器,我有超过 50 个结果,现在我想要的结果每页只有 10 个。我的搜索.php页面有以下代码

<?php 
 include('simple_html_dom.php');
 $search = $_GET['search']; 
 $html = file_get_html('http://mysite/'.$search.'.html');
foreach ( $html->find('div#song_html ') as $e ) {
$title= $e->find('div', 2)->plaintext;
 echo $title.'<br>'; 
}
?>

现在我使用此代码调用我的页面显示所有以上的 50 个结果..

 http://domain/search.php?search=Keyword
我想要每页 10 个结果 喜欢 &startrow=1 表示前 10 个结果

&startrow=2 表示第二个 10 个结果

 http://domain/search.php?search=Keyword&startrow=1 //page 1 with 10 result
 http://domain/search.php?search=Keyword&startrow=2 //page 2 with Next 10 result
 http://domain/search.php?search=Keyword&startrow=3 //page 3 with Next 10 result

你可以使用 array_slice() 来处理 DOM 解析器的结果...类似于以下代码的内容可以解决问题(未经测试的代码):

<?php 
include('simple_html_dom.php');
$page = array_key_exists('startrow', $_GET) ? (int)$_GET['startrow'] : 1;
$search = $_GET['search']; 
$html = file_get_html('http://mysite/'.$search.'.html');
$songs = $html->find('div#song_html ');
$paginationStart = min((10 * ((int)$page - 1)), (count($songs)-1));
$results = array_slice($songs, $paginationStart, 10);
foreach ( $results as $e ) {
    $title= $e->find('div', 2)->plaintext;
    echo $title.'<br>'; 
}
?>