简体   繁体   English

退出foreach循环,回显,然后继续foreach?

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

I have this little bit of script that just runs a query, retrieves all the categories in a database, and then lists them one on top of the other. 我有这个脚本,它只运行一个查询,检索数据库中的所有类别,然后将它们逐一列出。

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';
}

Basically, I need this code to loop through the first 10 results, then echo some HTML div and then pick back up off in the loop. 基本上,我需要此代码在前10个结果中循环,然后回显一些HTML div,然后在循环中重新选择。 I am however new and not sure how I would go about doing this. 但是,我是新手,不确定如何去做。 I was thinking of incorporating a counter but not sure if that would be the correct method. 我当时在考虑合并一个计数器,但不确定是否正确。

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