我正在尝试在1个循环之后添加2个引导类col-md-8
,然后在php while循环中添加2个col-md-4
类。这个过程在完整循环时应该是相同的。所以结果将如下所示:
我目前的代码如下:但它没有显示我需要的结果,无法了解循环的结果!
完整代码:
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class) ) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); if($x & 1) { $col='8' ; }else { $col='4' ; } ?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php $x++; } ?>
</div>
最新代码:
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class) ) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); $col=( (($x+1)/2)%2)? "8": "4"; ?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php $x++; } ?>
</div>
当前结果:
[![在此处输入图像说明] [2]] [2]
答案 0 :(得分:3)
请尝试此逻辑:$col = ((($i+1)/2)%2)?"8":"4";
如您所见,它会输出所需的结果。
The col for loop 0 is col4
The col for loop 1 is col8
The col for loop 2 is col8
The col for loop 3 is col4
The col for loop 4 is col4
The col for loop 5 is col8
The col for loop 6 is col8
The col for loop 7 is col4
The col for loop 8 is col4
The col for loop 9 is col8
The col for loop 10 is col8
The col for loop 11 is col4
The col for loop 12 is col4
The col for loop 13 is col8
The col for loop 14 is col8
The col for loop 15 is col4
The col for loop 16 is col4
The col for loop 17 is col8
The col for loop 18 is col8
The col for loop 19 is col4
您只需要在代码中替换
if($x & 1) {
$col = '8';
}else {
$col = '4';
}
与
$col = ((($x+1)/2)%2)?"8":"4";
答案 1 :(得分:0)
我会跟踪显示的变量。如果您首先将其定义为1,那么您将只显示第一次所需的第一列,然后它将切换到另一列。
您无需跟踪$x
。我不会称这是一个简单的数学问题,而这里的另一个OK答案不适合您的用例。
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php
$get_menu_class = mysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC");
$col = 4;
$displayed = 1;
while($menu_class_result = mysqli_fetch_array($get_menu_class) ) {
$menu_class_name = htmlspecialchars($menu_class_result['pcat_name']);
$menu_class_image = htmlspecialchars($menu_class_result['pcat_image']);
?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php
if($displayed){
switch($col){
case 4:
$col = 8;
break;
case 8:
$col = 4;
break;
default:
$col = 4;
}
$displayed = 0;
} else {
$displayed = 1;
}
}
?>
</div>