How I add a CSS class to first element of an array in PHP? -


i trying select images mysql , need store result in array.

this while loop far:

// fetch 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, want add css class named active first element of $gallery array. class need add line <div class='item'>\n"; class="item active"

update:

$count = 0;  // fetch 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 tell me how this?

thank you.

you can add 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++; } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -