Jump to content

Function in foreach


selenin

Recommended Posts

Hello, now I made a foreach loop and I wanted to stop it with count < 10 but I don't get it, that's my code:

 

foreach ($results as $res) {
     
      echo "<li> <a href='movie.php?mid=".$res->imdbid()."&engine=".$_GET["engine"]."'>".$res->          title()." (".$res->year().")</li>";

}

 

Now I try that it shows only when there are more than 10 only the first 9

Link to comment
https://forums.phpfreaks.com/topic/203979-function-in-foreach/
Share on other sites

The code you posted doesn't have any limiting logic. Try this:

<?php
$i = 0;
foreach ($results as $res) {
    echo "<li> <a href='movie.php?mid=".$res->imdbid()."&engine=".$_GET["engine"]."'>".$res->          title()." (".$res->year().")</li>";
    $i++;
    if ($i >  break;
}?>

 

Ken

 

}

Link to comment
https://forums.phpfreaks.com/topic/203979-function-in-foreach/#findComment-1068362
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.