Limit results in PHP using while loop
<p>I have a list of returned subcategories. I only have access to the template display and not to the MySQL query so no, I can't limit the query.</p>

<p>Right now all the results are returned. I would like to limit the list to 5, and then add a "more" link if there are more than 5 results.</p>

<p>I know I did this wrong because I don't think <code>count</code> is actually tied to the <code>foreach</code>: </p>

<pre><code>&lt;ul class="sub-categories"&gt;
    &lt;?php
    while (count($category-&gt;getChildren()) &lt;= 5) { // line I added for while loop
        foreach ($category-&gt;getChildren() as $child) {
            if (!$child-&gt;totalItemCount())
                continue;

            $link = $this-&gt;app-&gt;route-&gt;category($child);
            $item_count = ($this-&gt;params-&gt;get('template.show_sub_categories_item_count')) ? ' &lt;span&gt;('.$child-&gt;totalItemCount().')&lt;/span&gt;' : '';
                echo '&lt;li&gt;&lt;a href="'.$link.'" title="'.$child-&gt;name.'"&gt;'.$child-&gt;name.'&lt;/a&gt;'.$item_count.'&lt;/li&gt;';
        }
    } // line I added for while loop
    ?&gt;
&lt;/ul&gt;
</code></pre>
