Reputation: 2763
I have a banner that used from bootstrap and, the first slider on this banner should have class='item active'
the rest of the sliders should have class='item'
I am getting my sliders from my database
so far that what I try to do.
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
while ($b = $results->fetch_array()) {
$bannerImages = array($b['image']);
foreach ($bannerImages as $image) {
if ($image[0]) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
}
?>
still not working as expected
var_dump
array (size=1)
0 => string '06.jpg' (length=6)
array (size=1)
0 => string '03.jpg' (length=6)
I see that the var_dump
is 0
for all items what did I do wrong here?
Upvotes: 0
Views: 59
Reputation: 795
This is a fix to the old code:
<?php
$getBanner = $db->prepare("SELECT image FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$is_first = true;
while ($b = $results->fetch_array()) {
if ($is_first) {
echo '<div class="item active">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
$is_first = false;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>
Upvotes: 1
Reputation: 795
Change the code like this:
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$rows = $results->fetch_all(MYSQLI_ASSOC);
$ind = 0;
foreach ($rows as $image) {
if ($ind == 0) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
$ind++;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>
Upvotes: 0