php如何限制结果

时间:2013-09-23 23:27:33

标签: php limit explode

我有那个代码,我想将结果限制为10 max。 我怎么能这样做?

    <?php 
    $actors = rtrim($movie_info[0]->actors, ", "); 

    if(stristr($actors, ",")) {
        $actors = explode(",", $actors);
        array_walk($actors, 'add_url_on_tags', '/actor');
        echo ul($actors, array("class" => "genres2"));
        echo '<div style="clear:both;"></div>';
    }elseif(!empty($actors)) {
            echo '<a href="/actor/'.url_title($actors).'">'.$actors.'</a><br />'; 
    }else{
        echo 'empty';
    }

    ?>

谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

你要找的是$ actors = array_slice($ actors,0,10);

<?php 
$actors = rtrim($movie_info[0]->actors, ", "); 

if(stristr($actors, ",")) {
    $actors = explode(",", $actors);
    $actors = array_slice($actors, 0, 10);
    array_walk($actors, 'add_url_on_tags', '/actor');
    echo ul($actors, array("class" => "genres2"));
    echo '<div style="clear:both;"></div>';
} elseif(!empty($actors)) {
        echo '<a href="/actor/'.url_title($actors).'">'.$actors.'</a><br />'; 
} else{
    echo 'empty';
}

?>

答案 1 :(得分:0)

你应该在你的SQL中做,而不是只通过上面的代码挑选你的结果。

select
    *
from
    yourTable
where
    someColumn=SomeCondition
order by
    someColumn
limit 10
^^ This is the clause to add.

只会带回前十个结果

然后你可以通过添加一个这样的值来使用它进行分页:

limit 10,10

这将带回十个结果,但跳过前十个(基本上显示结果11-20