ankit suthar ankit suthar - 5 months ago 11
PHP Question

Change data in the while loop while looping

I am working in php and right now i am applying while loop to my code.
i am fetching data from database.
Now i have to apply that data on one Div in the page.

My problem is that "div class="item active"" in the loop it take every time the active class. now i want to change that like after 1st loop process when second gone start i want to change that div to this "div class="item"" .

I am little new at this looping process so i am not able to solve this. need help. Thanks.

<?php

$sqlEvent = "SELECT * FROM eventdata WHERE id='$id'";

$resultEvent = mysqli_query($connection, $sqlEvent);

if (mysqli_num_rows($resultEvent) > 0) {

while ($rowEvent = mysqli_fetch_array($resultEvent)) {

?><div class="item active"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
<div class="carousel-caption wedding-area">
<h2><?php echo $rowEvent['eventTitle']; ?></h2>
<h4><?php echo $rowEvent['eventDate']; ?></h4>
<h4><?php echo $rowEvent['eventTime']; ?></h4>
<div class="details hidden-xs">
<p><?php echo $rowEvent['eventDesc']; ?> </p>
</div>
<a href="#" class="btn btn-default" role="button">Read More</a> </div>
</div><?php

}

}

?>

Answer

Use a counter to check first iteration

$i = 1;// initialized counter
while ($rowEvent = mysqli_fetch_array($resultEvent)) {
    if ($i == 1) {// check for first iteration
        ?><div class="item active"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
    <?php } else { ?>

            <div class="item"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
                <?php
            }
            $i++;// increase counter

        ?>