簡體   English   中英

退出foreach循環,回顯,然后繼續foreach?

[英]Break out of foreach loop, echo, then continue the foreach?

我有這個腳本,它只運行一個查詢,檢索數據庫中的所有類別,然后將它們逐一列出。

if ($all_categories) {
 foreach ($all_categories as $cid => $arr) {

 $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br />';
 }
}
} else {
$sidebar = 'There are no categories yet';
}

基本上,我需要此代碼在前10個結果中循環,然后回顯一些HTML div,然后在循環中重新選擇。 但是,我是新手,不確定如何去做。 我當時在考慮合並一個計數器,但不確定是否正確。

if ( $all_categories ) {
    $count = 1;

    foreach ( $all_categories as $cid => $arr ) {
        $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br />';

        if ( 10 == $count ) {
            $sidebar .= 'This code fires after the 10th result is displayed.';
        }

        $count++;
    }
} else {
    $sidebar = 'There are no categories yet';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM