HOw to exit for each loop

I am using Foreach loop to print fields from database.

But i have condition that i want to print only top 5 data.

Or if database is blank then it will show two blank field but only one time .

PLz help is there any to do .

I tried it using break it change the design of my html .

hi

use break;

Not quite sure what U want to to, but here is some idea:

    • continue, will skip the current iteration block, but continue on with the rest of the loop.
    • break, will exit foreach completely

or if anything does not fit your need U can do some custom counting:

count iterations and until count reaches 5 print what U need, after that don’t do anything

This should help also:

http://php.net/manual/en/control-structures.continue.php

Hi , use select query to retrieve the top 5 records , there are 2 ways either you use simple foreach inside your view template or you can use limit 0,5 like SELECT * FROM tablename ORDER BY field LIMIT 5; and inside foreach you need a counter , here is the code





$i = 1;

foreach($result as $val)

{

   if($i ==5)

{

   break;

}

else

{

echo $val->yourvalue; 

}

$i++;

}