我有循环
<div class="btn-demo">
<?php
foreach ($getSubCat as $value):
$getSubCat1 = $conn->query("select name from tbl where id = '$value'")->fetch_object();
?>
<button class="btn btn-danger"><?=$getSubCat1->name?></button>
<?php endforeach; ?>
</div>
现在这将返回带有btn-danger
的所有按钮。现在,如果我想申请
按钮上的那些课程。意味着我想要显示彩色按钮,这样就可以在每次循环运行时更改类。
答案 0 :(得分:1)
将数组和存储类放在
上<div class="btn-demo">
<?php
$class = array('btn-warning','btn-success','btn-info');
$i = 0;
foreach ($getSubCat as $value):
$getSubCat1 = $conn->query("select name from m_subcategory where id = '$value'")->fetch_object();
?>
<button class="btn <?=$class[$i]?> btn-quirk btn-stroke"><?=$getSubCat1->name?></button>
<?php
$i++;
endforeach;
?>
</div>
答案 1 :(得分:0)
不能比那更简单:
<div class="btn-demo">
<?php
$class= ['btn-success','btn-info','btn-warning'];
foreach ($getSubCat as $k => $value):
$getSubCat1 = $conn->query("select name from tbl where id =
'$value'")->fetch_object();
?>
<button class="btn <?=$class[$k] ?>"><?=$getSubCat1->name?></button>
<?php endforeach; ?>
</div>
答案 2 :(得分:0)
如果您在表格中有状态,请执行以下操作
//状态是&#34;危险&#34;或=&#34;成功&#34;
<div class="btn-demo">
<?php
foreach ($getSubCat as $value):
$getSubCat1 = $conn->query("select name,status from tbl where id = '$value'")->fetch_object();
$btn_class = "btn btn-".$getSubCat1->status;
?>
<button class="<?php echo $btn_class; ?>"><?=$getSubCat1->name?></button>
<?php endforeach; ?>
</div>