How to limit foreach loop to 3 results as icons in PHP? -


i have piece of code showing results icons, shows 50 icons in each loop. searched many ways change 3 icons in each loop unsuccessfully. how can that?

foreach ($categoryitems $item) {       if ($item['categoryid'] != $category['id'])         continue;      $itemxml = $items->finditeminxml($item['itemid']);     $subcontent .= "<a class=\"thumbnail\" ><img src=\"images/gameicons/".$items->geticon($itemxml).".png\" alt=\"item icon\" align=\"top\" />"; }  $subcontent .= "</td> <td class=\"tablepreview ui-state-hover\"> <a href=\"showmore\">more information</a> </td> </tr>"; 

since have continue condition, suggestion use for loop less relevant. instead can use counter variable count how many icons have been printed. once counter reach 3 breaks loop. (didn't tested it)

$printedicons = 0; foreach ($categoryitems $item) {       if($printedicons == 3)         break;     if ($item['categoryid'] != $category['id'])         continue;      $itemxml = $items->finditeminxml($item['itemid']);     $subcontent .= "<a class=\"thumbnail\" ><img src=\"images/gameicons/".$items->geticon($itemxml).".png\" alt=\"item icon\" align=\"top\" />";     $printedicons++; } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -