1

I am trying to select some images from mysql and need to store result in an array.

This is my WHILE loop so far:

// Fetch all the records:
while ($stmt->fetch()) {
        $result  = "<div class='item'>\n";
        $result .= "    <div class='gallery_image'>\n";
        $result .= "        <a class='thumbnail lightbox' title='{$db_restaurant_name}' rel='gal' target='_blank' href='{$image_path}{$image}'>\n";
        $result .= "            <div class='img-holder' style='background-image:url({$image_path}{$image})'></div>\n";
        $result .= "        </a>\n";
        $result .= "    </div>\n";
        $result .= "</div>\n";  
        $gallery[] = $result;   
    }  
}

My question is, I want to add a CSS class named active to first element of this $gallery array. This class need to be add to this line <div class='item'>\n"; Like this class="item active"

UPDATE:

$count = 0; 
// Fetch all the records:
while ($stmt->fetch()) {
    if($image_type == 0) {
        $class = '';
        if($count === 0) {
            $class = ' active';
        }

        $result  = "<div class='item {$class}'>\n";
        $result .= "    <div class='gallery_image'>\n";
        $result .= "        <a class='thumbnail lightbox' title='{$db_restaurant_name}' rel='gal' target='_blank' href='{$image_path}{$image}'>\n";
        $result .= "            <div class='img-holder' style='background-image:url({$image_path}{$image})'></div>\n";
        $result .= "        </a>\n";
        $result .= "    </div>\n";
        $result .= "</div>\n";  
        $gallery[] = $result;   

    }
    $count++;       
}  

Can anybody tell me how I do this?

Thank you.

2
  • @Jessica, In my case, I can not use CSS. Thats why I am looking for solution in PHP Commented Jul 22, 2015 at 15:18
  • Ugly way is to keep the count of loop. Since, it's an ugly way, I won't even try to right the answer. Are you getting the index or some value from database which says that gallery row is active? Commented Jul 22, 2015 at 15:22

2 Answers 2

2

You can add a counter variable:

$count = 0;
while ($stmt->fetch()) {
    $class = '';
    if($count === 0) {
        $class = ' active';
    }
    $result  = "<div class='item" . $class . "'>\n";
    $result .= "    <div class='gallery_image'>\n";
    $result .= "        <a class='' title='' href='{$image_path}{$image}'>\n";
    $result .= "            <div class='' style='background-image:url({$image_path}{$image})'></div>\n";
    $result .= "        </a>\n";
    $result .= "    </div>\n";
    $result .= "</div>\n";  
    $gallery[] = $result;  
    $count++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

this add active class to all elements
Did you notice the "$count++;" at the end?
$count is increased inside the loop? Then it should be 0 only the first time the loop is entered. $class is reset to an empty string everytime, so it is impossible that all elements get the active class.
Can you update your question with your current code?
0

The easiest way to do this is to create a counter variable like :

$i = 0;
while ($stmt->fetch()) {
    $result  = "<div class='item " . (0 == $i ? "active" : "") . "'>\n";
    // ...
    $i ++;
} 

Comments

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.