0

我有那个代码,我想将结果限制为最多 10 个。我怎样才能做到这一点?

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

    ?>

感谢您的帮助!

4

2 回答 2

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

?>
于 2013-09-23T23:32:39.557 回答
0

您可能应该在您的 SQL 中执行此操作,而不是只通过上面的代码挑选您的结果。

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

只会带回前十个结果

然后,您可以将其用于分页,方法是再添加一个值,如下所示:

limit 10,10

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

于 2013-09-23T23:33:42.173 回答