user8774607
user8774607

Reputation:

change class every time loop runs

I Have Following Loop

<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>

Now this will return all button with btn-danger.now if i want to apply

those class on buttons. means i wants to show colourful buttons so is this possible to change class every time loop runs.

Upvotes: 1

Views: 496

Answers (3)

Nandhi Kumar
Nandhi Kumar

Reputation: 353

If you have status in the table do the below thing

// status is "danger" or = "success"

<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>

Upvotes: 0

CodexVarg
CodexVarg

Reputation: 510

cant be any simpler than that:

<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>

Upvotes: 0

TarangP
TarangP

Reputation: 2738

Take array And store classes on that

<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>

Upvotes: 1

Related Questions