Goedemorgen,

Ik wil een top 10 van landen als output hebben.
Het is mij gelukt om een For Each loop na 10x af te breken. Echter, omdat de output ook nog eens filter (en een aantal landen uitsluit), is de output minder dan 10. In onderstaande voorbeeld is de output nooit 10 zodra er er wat bezoek is uit Amerika, Canada, China of Mexico. Toch wil ik deze uitsluiten en als output een top-10 hebben.


    <table class="table" >
        <thead>
            <tr>
                <th><?=l('Top 10 landen')?></th>
                <th><?=l('Bezoek')?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach(array_slice($countries, 0, 10) as $country => $cdata) 
            if($country != 'United States' && $country != 'Canada' && $country != 'China' && $country != 'Mexico') {
            ?>
            <tr>
                <td><?=$country?></td>
                <td><?=$cdata['views']?></td>
            </tr>           
            <?php } ?>
        </tbody>
    </table>
 



Ik snap de logica dat het andersom moet, maar ik weet niet hoe ik dit moet programeren. Kan iemand mij helpen?

Alvast bedankt!
Probeer het zo eens :


     <table class="table" >
        <thead>
            <tr>
                <th><?=l('Top 10 landen')?></th>
                <th><?=l('Bezoek')?></th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $teller = 0;
            foreach($countries as $country => $cdata)
            if($country != 'United States' && $country != 'Canada' && $country != 'China' && $country != 'Mexico' && $teller <= 10) {
            $teller++;
            ?>
            <tr>
                <td><?=$country?></td>
                <td><?=$cdata['views']?></td>
            </tr>          
            <?php } ?>
        </tbody>
    </table>


Dank je, Pipo. Ik moest alleen nog even de array_slice weghalen en bij $teller=1 beginnen:


            <?php
            $teller = 1;
            foreach($countries as $country => $cdata)
            if($country != 'United States' && $country != 'Canada' && $country != 'China' && $country != 'Mexico' && $teller <= 10) {
            $teller++;
            ?>

Reageren