I’m using a while loop to populate tabs with PHP. If I assign active while it’s in the loop then they are all active. This much I understand. If I only want the first item / tab to have the class="tab-pane fade p-2 –>active<–" where should the script be changed?
while($row = $result->fetch_assoc() ) {
$id = $row['id'];
$in = $row['initial'];
$name = $row['name'];
echo "<div class="tab-pane fade p-2 active" id="$in" role="tabpanel" aria-labelledby="$in-tab">"; // make tabs
2
Answers
Track an indicator that you’ve already rendered the "first" tab. Something as simple as:
Then within the loop, set it to
true
after rendering the "first" tab, conditionally including theactive
class:First: write HTML as HTML, not using strings.
You’ll need to use a index, like: