简体   繁体   中英

php how to limit the result

i have that code, and i want to limit the result to 10 max. how can i do that?

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

    ?>

thank for your help!

What you're looking for is $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';
}

?>

you should probably do it in your SQL instead of only cherry picking your results via the code above.

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

Will only bring back the first ten results

you can then use it for pagination, by adding one more value like this:

limit 10,10

This will now bring back ten results, but skip the first ten (basically showing results 11-20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM