我的代码中有这个循环:
$results = modleftHelper::getNear();
$i = 0;
foreach($results as $result){
echo '<div class="filter-div" data-filter="'.$result->near.'">';
echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
$i++;
}
我想在这个div中每3,6,9等添加一个明确的类:
<div class="filter-div" data-filter="'.$result->near.'">';
有任何想法或建议吗?感谢。
答案 0 :(得分:2)
试试这个:
$results = modleftHelper::getNear();
$i = 0;
foreach( $results as $result ) {
if ( $i % 3 == 0 ) {
echo '<div class="filter-div clear" data-filter="'.$result->near.'">';
} else {
echo '<div class="filter-div" data-filter="'.$result->near.'">';
}
echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
$i++;
}
答案 1 :(得分:1)
尝试
$i = $j = 0;
foreach($results as $result){
if($i++%3==0)
{
echo '<div class="filter-div clear" data-filter="'.$result->near.'">';
} else {
echo '<div class="filter-div" data-filter="'.$result->near.'">';
}
echo '<label id="lbl_type'.$j++.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
}
考虑Iam使用$i
检查条件,$j
检查循环变量
答案 2 :(得分:0)
这将有所帮助
$results = modleftHelper::getNear();
$i = 0;
foreach($results as $result){
$i++;
//check the incremental value
if($i%3==0)
{
echo '<div class="filter-div" data-filter="'.$result->near.'">';
$i=0;
}else{
//add youe false condition ghere
echo '<div class="filter-div" data-filter="">';
}
echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
}
答案 3 :(得分:0)
试试这个:
$results = modleftHelper::getNear();
$i = 0;
foreach($results as $result){
$clear = ($i+1 % 3) == 0 ? "clear" : "";
echo '<div class="filter-div' . ' ' . $clear .'" data-filter="'.$result->near . '">';
echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
$i++;
}
答案 4 :(得分:0)
$results = modleftHelper::getNear();
$i = 0;
foreach($results as $result){
if($i%3==0){$last_class="clear";}else{$last_class="";}
echo '<div class="filter-div '.$last_class.'" data-filter="'.$result->near.'">';
echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
echo '</div>';
$i++;
}
试试这段代码。