0

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

                }

                }

                ?>
3
  • Use flag is_first and change it after first iteration. Thousands of such questions. Commented Jun 24, 2016 at 12:39
  • Possible duplicate of How to get specific column data SQL for first row only in PHP? Commented Jun 24, 2016 at 12:41
  • Not this time @u_mulder are you blind ?? Commented Jun 24, 2016 at 12:44

3 Answers 3

1

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

        ?>
Sign up to request clarification or add additional context in comments.

2 Comments

let me try this one
Please up the question if it is.
0

You want only the first div of the loop has class "active"... then this code should work for you

Just set variable = "actìve" then at the end of the body loop set = "".

In each iteration print the variable in the class attribute of your div

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

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

if (mysqli_num_rows($resultEvent) > 0) {
    $classActive = "active";
    while ($rowEvent = mysqli_fetch_array($resultEvent)) {
        ?>
            <div class="item <?php echo $classActive; ?>"> 
                <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
        $classActive = "";
    }

}

Comments

0

use pdo functions they are easier to use

http://php.net/manual/pl/pdostatement.fetchall.php

and always escape values in sql query!

<?php 


             $sth = $dbh->prepare("SELECT * FROM eventdata WHERE id=?);
             $sth->execute([$id]);
             foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $i => $rowEvent) {

            ?><div class="item  <?php if(1 === $i) echo '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

            }


            ?>

1 Comment

Instead of posting links as answer add some text to explanation how this answer help to OP in fixing current issue.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.