How can I have a class be applied only once in a loop?

for loop
execute a statement only once in a loop
execute a statement only once in a loop python
how to call a method only once in java
for loop javascript
for loop syntax
for loop example
html for loop

I'm trying to use a Bootstrap carousel in Wordpress, so I need to have a loop. However, the slider script requires the first slide to have a special class, and can't figure how to apply that class to the first iteration of the loop and only to it (in fact the class will rotate throughout the slides when the carousel is working, but the html needs to have first slide with active class and then all other slides without that class).

here is my code so far:

<div id="myCarousel" class="carousel slide">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>
    <!-- Wrapper for Slides -->
    <div class="carousel-inner">
        <?php if( have_rows('slide') ): ?>
        <?php while( have_rows('slide') ): the_row();
        // vars
        $img = get_sub_field('slide_image');
        $desc = get_sub_field('slide_text');
        $title = get_sub_field('slide_title');
        $url = $img['url'];
        $size = 'slider';
        $thumb = $gal['sizes'][ $size ];
        ?>
        <div class="item active">
            <!-- Set the first background image using inline CSS below. -->
            <div class="fill" style="background:url('<?php echo $img; ?>') no-repeat; background-size:cover;"></div>

            <div class="carousel-caption">
            <h2><?php echo $title; ?></h2>
            <div class="desc"><?php echo $desc; ?></div>
            </div>
        </div>
        <?php endwhile;?>               
        <?php endif;?>
    </div>
    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="icon-prev"></span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="icon-next"></span>
    </a>
</div>  

and for reference, the original pure HTML code is located at https://github.com/IronSummitMedia/startbootstrap/blob/gh-pages/templates/half-slider/index.html

so, in short, what I need is to have this line:

<div class="item active">

only once, but all the other iterations should be

<div class="item">

It would be something like How to put an class on specific (first element) inside loop?, only that in PHP and WordPress, I tried to follow that answer but couldn't understand it

Any help really appreciated

If you want just one occurrance, just use an $i variable.

<?php
// This sets the value to 0
$i = 0;
while( have_rows('slide') ): the_row();
        // vars
        $img = get_sub_field('slide_image');
        $desc = get_sub_field('slide_text');
        $title = get_sub_field('slide_title');
        $url = $img['url'];
        $size = 'slider';
        $thumb = $gal['sizes'][ $size ];
        ?>
        <!-------------------------------->
        <!-- THIS $i checks if equals 0 -->
        <!-------------------------------->
        <div class="item<?php if($i==0) { ?> active<?php } ?>">
            <!-- Set the first background image using inline CSS below. -->
            <div class="fill" style="background:url('<?php echo $img; ?>') no-repeat; background-size:cover;"></div>

            <div class="carousel-caption">
            <h2><?php echo $title; ?></h2>
            <div class="desc"><?php echo $desc; ?></div>
            </div>
        </div>
        <?php
        // This will increment $i so $i should not 
        // equal 0 except on the first loop
        $i++;
endwhile; ?>

Advances in Web-Age Information Management: 6th International , That is, they can not create loops in the derivation graph. Now let's look at the rdfs8 rule: rdfs8: (uuu rdf:type rdfs:Class) → (uuu rdfs:subClassOf rdfs:Resource) Under the OSA, sub-class. Therefore, this rule can be applied only once after the  Note: you can solve this task more elegantly with a while loop. However, I haven’t written a while loop tutorial yet, which is why I went with the for loop + break solution! Test Yourself! It’s time to test whether you have managed to master the if statement, the for loops and the combination of these two!

You can use $wp_query->current_post to get the index position in The Loop and then only print out "active" when the $wp_query->current_post == 0

Edit: realized you said "a loop" and not "the loop", you can just use an variable and if statement to check if it is the first iteration of your loop or not.

$show_active = true;
while( have_rows('slide') ): the_row();
    if ( $show_active ){
        $active = 'active';
        $show_active = false;
    } else {
        $active = '';
    }
endwhile;

Hardcore Java: Secrets of the Java Masters, When the value of idx is 555, the break statement is hit and the loop exits. statement in that it applies only to the loop in which the continue is enclosed. Point in Matrix"); LOGLINE2: Once you have declared the label, you can use it in a break or a public class SyntaxIssues { This is the Title of the Book, eMatter Edition  In addition to the Equals method, strings also provide the Compare method, which gives you information about the relative order of strings instead of just a test for equality. This method is preferable to the <, <=, > and >= operators, for the same reasons as discussed above–to avoid C# problems.

I have used:

<div class="carousel-inner">
    @foreach($banners as $banner)
    <div class="carousel-item @if($banner->id == 1) active @endif">
        <img class="d-block w-100" src="{{ Storage::disk('local')->url($banner->image) }}" alt="{{ $banner->title }}">
        <div class="carousel-caption d-none d-md-block">
            <h1>{{ $banner->title }}</h1>
            <p>{{ $banner->body }}</p>
        </div>
    </div>
    @endforeach
</div>

Works like a breeze!

Java in Two Semesters: Featuring JavaFX, Once an Iterator object has been created, a while loop can be used to iterate Since we have specified the Iterator object to retrieve String objects, we know that in a variable, as the next method should be called only once within the loop. to remove current item The IteratorDemo class gathers this code into a complete​  1.Decide what work must be done inside the loop, 2.specify the loop conditions, 3.determine the loop type, 4.set up variables for entering the loop for the first time, 5.process the loop after the loop has finished, 6. trace the loop with typical examples, then 7. implement the loop in java. We distinguish between two major loop types.

Computer Science, However, for P-solvable loops with assignments only our (of different degrees) among the program variables by the application of our method only once. so–​called simple loops having solvable mappings with positive rational eigenvalues. variables for a wider class of recurrence equations (assignment statements). In a _____ loop, the loop body might never execute because the question controlling the loop might be false the first time it is asked. Post-Test In a ____________ loop , the loop body executes at least one time because the loop control variable is not tested until after one iteration.

Parliamentary Papers: 1850-1908, I only once remained cu duty till 2 a.m. I forget what was the cause of this. The next carriage, a third-class bogie(m-riage, was on the up loop with both It was a similar engine to mine, My brake'was working all right, and I used it to stop T4. Such a loop, just like any other, can be stopped with the break directive. If we don’t want to do anything in the current iteration and would like to forward to the next one, we can use the continue directive. break/continue support labels before the loop. A label is the only way for break/continue to escape a nested loop to go to an outer one.

How do I ensure a piece of code runs only once?, public class InputController { //declare delegate type: //<accessbility> delegate <​return type> However, you aren't normally running a multithreaded event loop and pressing the buttons of If you want to make code only run once, you could have code in the method overwrite the The same can be applied for Released. c) initializations of a for loop control variable can occur before the for loop executes and not in the loop itself d) the increment portion of a for header can be a decrement a) statements proceeding a for and statements in the body of a for should typically be merged into the for header.

Comments
  • Tried your code but it adds the active class to all iterations
  • Did you unset $i at the end? It should unset after the first loop.
  • yes, as a matter of fact I even tried copy pasting your whole code just to be sure, but always the same result
  • If this next edit doesn't work, I don't know what will! That first one should have worked without a hitch.
  • the problem is my knowledge is quite limited and I don't know how to do that