我有像这样的foreach循环
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
$sn_count++;
}
echo $html;
此代码输出如下
1 第一点描述
2 第二点描述
等...
现在我想在每2,4,6,......之后展示广告。我的意思是在2-3,4-5,6-7之间...... 任何人都可以帮我代码吗? 谢谢
答案 0 :(得分:1)
这应该这样做
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count % 2 === 0){
$html .= get_ad_code()
}
$sn_count++;
}
echo $html;
答案 1 :(得分:1)
您需要使用modulus
$sn_count = 1;
$html = '';
$display_ad_every = 3;
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count++ % $display_ad_every == 0)
{
// append ad code here
}
}
echo $html;