我试图从mysql中选择一些图像并需要将结果存储在一个数组中。
到目前为止,这是我的WHILE循环:
// 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;
}
}
我的问题是,我想在CSS
数组的第一个元素中添加一个名为active
的{{1}}类。此类需要添加到此行$gallery
,例如<div class='item'>\n";
更新:
class="item active"
有谁能告诉我这是怎么做到的?
谢谢。
答案 0 :(得分:2)
您可以添加计数器变量:
$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++;
}
答案 1 :(得分:0)
最简单的方法是创建一个计数器变量,如:
$i = 0;
while ($stmt->fetch()) {
$result = "<div class='item " . (0 == $i ? "active" : "") . "'>\n";
// ...
$i ++;
}