I'm using Twitter Bootstrap , I want to add the active class in my for loop, found some articles with <nav> element but I still couldn't get it to work.
Here is my code:
<?php
$tablename = "banners";
$type= 'slider';
$displayBanner = new Display($tablename);
$BannerDataDisplay = $displayBanner->getAllDataByStatusType($type);
//`id`, `bannerName`, `bannerURL`, `status`, `createdBy`, `CreatedDate`
//var_dump($BannerDataDisplay);
echo '
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
';
for($i=0;$i<count($BannerDataDisplay);$i++){
echo' <div class="item active">
<img src="app/'.$BannerDataDisplay[$i]['bannerURL'].'" alt="Slide">
</div>';
}
echo '
</div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
';
?>
PS: If you can solve this, please use this variable: $BannerDataDisplay[$i]['bannerURL'] <- that is the url of my image
OK i Solved it , had to search some related articles mostly from Wordpress:
for ($i=0; $i < count($BannerDataDisplay); $i++) {
if ($i==0) {
echo ' <div class="item active">
<img src="app/' . $BannerDataDisplay[$i]['bannerURL'] . '" alt="Slide">
</div>';
} else {
echo '<div class="item">
<img src="app/' . $BannerDataDisplay[$i]['bannerURL'] . '" alt="Slide">
</div>';
}
}
For current image here you go:
$('.sliderTrigger').on('click', function() {
var index = $(this).attr('data-slide-to');
console.log(index);
$('#myGallery .carousel-inner .item').removeClass('active');
$('#myGallery .carousel-inner .item').each(function(i,element) {
console.log(i)
if(i == index) {
$(this).addClass('active');
}
})
})
Related
enter image description hereHere is the output I have tried this code but the carousel is not working properly...The first image is shown properly but the second one is shown below the first one and not in slides....Here is my code..
<div class="banner">
<?php
mysqli_set_charset($conn, 'utf8');
$query=mysqli_query($conn,"select * from post_tbl where status='Approved' Order by pid DESC");
while($row=mysqli_fetch_array($query))
{
?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="3"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<div class="carousel-caption">
<h3><?php echo $row['title']; ?>
</h3>
<div class="read">
Read More
</div>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<?php } ?>
</div>
You will need 2 loops. One to know how many rows you are going to draw. This will be used to build your ol DOM.
The second to loop your actual div content where your select contains the necessary information.
<div class="banner">
<?php
mysqli_set_charset($conn, 'utf8');
$query = mysqli_query($conn, "select * from post_tbl where status='Approved' Order by pid DESC");
?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<?php for ($numElements = 0; $numElements < mysqli_num_rows($query); $numElements++) : ?>
<li data-target="#carouselExampleIndicators"
data-slide-to="<?php echo $numElements ?>" <?php echo($numElements == 0 ? 'class="active"' : '') ?> ></li>
<?php endfor; ?>
<div class="carousel-inner" role="listbox">
<?php $rows = mysqli_fetch_array($query); ?>
<?php foreach ($rows as $row) : ?>
<div class="carousel-item <?php echo($row == reset($rows) ? 'active' : '') ?>">
<div class="carousel-caption">
<h3><?php echo $row['title']; ?>
</h3>
<div class="read">
Read More
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
This should set your DOM correctly.
If you are already familiar with OOP concepts this gets way more organised and protected and would highly advice to follow that route instead.
Also there are plenty of threads around warning about sql injection so, if you are thinking on having a variable to dynamically adjust your query, please look into those first
As an introduction I can leave you with this thread
I have the following codes which pull images from a database and show in Bootstrap's - carousel.
<div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-ride="carousel">
<ol class="carousel-indicators">
<?php
$carouselImageSql = "SELECT * FROM indexPageElements WHERE carouseImage = '1'";
$carouselImageResult = mysqli_query($conn, $carouselImageSql);
$i = 0;
while($carouselImageRow = mysqli_fetch_array($carouselImageResult)){
$imageLocation = $carouselImageRow['carouseImageLocation'];
echo '<li data-target="#carouselExampleIndicators" data-slide-to="" ></li>';
?>
</ol>
<div class="carousel-inner" role="listbox">
<?php
if($i == 0){
echo '<div class="carousel-item active">
<img class="d-block img-fluid" src="images/'.$imageLocation.'" alt="First slide">
</div>';
}else{
echo '<div class="carousel-item">
<img class="d-block img-fluid" src="images/'.$imageLocation.'" alt="First slide">
</div>';
}
$i += 1;
}
?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
My Issue
My problem here is, once all the images pulled out from database, carousel stop showing those pictures again. Does anyone knows how to grab the picture from database and show it in carousel but at the same time loops infinitely?
Edit 1
I did the following to loop it for 10 times. But still it is only for 10 times
<div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-ride="carousel">
<ol class="carousel-indicators">
<?php
$carouselImageSql = "SELECT * FROM indexPageElements WHERE carouseImage = '1'";
$carouselImageResult = mysqli_query($conn, $carouselImageSql);
$carouselImageCount = mysqli_num_rows($carouselImageResult);
$imageLocation = array();
while($carouselImageRow = mysqli_fetch_array($carouselImageResult)){
$imageLocation[] = $carouselImageRow['carouseImageLocation'];
echo '<li data-target="#carouselExampleIndicators" data-slide-to="" ></li>';
}
?>
</ol>
<div class="carousel-inner" role="listbox">
<?php
$i = 0;
for($x = 0; $x <= 10; $x++){
foreach($imageLocation as $loc){
if($i == 0){
echo '<div class="carousel-item active">
<img class="d-block img-fluid" src="images/'.$loc.'" alt="First slide">
</div>';
}else{
echo '<div class="carousel-item">
<img class="d-block img-fluid" src="images/'.$loc.'" alt="First slide">
</div>';
}
$i += 1;
}
}
?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
What I mean is to turn on the wrap option, as below:
$("#carouselExampleIndicators").carousel({ wrap: true });
https://getbootstrap.com/docs/4.0/components/carousel/#options:
Wrap is whether the carousel should cycle continuously or have hard stops.
you will need JQuery and Bootstrap JS.
You can try to activate your carousel with this short javascript.
<script>
$(document).ready(function(){
// Activate Carousel
$("#carouselExampleIndicators").carousel();
});
</script>
I store my pictures that are uploaded on my website in the database, then when I load them in my website, I store them in an array. My problem is I want to display the pictures in a slideshow but because sometimes the array not always is the same, I cannot display it right!
I tried to do if statement but then the function of slideshow didn't work at all!
Also, My array works fine! just having a problem with displaying
$photosarray= array(); //array to store photos names
$count=0;
while ($row2=mysqli_fetch_array($res2))
{
$image=$row2['img_name'];
$photosarray[$count]=$image;
$count=$count+1;
}
echo'
<td>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="../photo/'.$photosarray[0].'" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="../photo/'.$photosarray[1].'" class="d-block w-100" alt="...">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</td>';
echo '
<p class="font-weight-bold">click the image to view in full
size.</p>
</div>
</tbody>
';
I expect that my slideshow works depending on how many images in the array! so is the array has only 2 images then the slideshow should only have 2 slides!
You need to display in a loop, change:
<div class="carousel-item active">
<img src="../photo/'.$photosarray[0].'" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="../photo/'.$photosarray[1].'" class="d-block w-100" alt="...">
</div>
to
foreach($photosarray as $photo){
print('
<div class="carousel-item">
<img src="../photo/'.$photo.'" class="d-block w-100" alt="...">
</div>
');
}
try this
<?php
$photosarray= array(); //array to store photos names
$count=0;
while ($row2=mysqli_fetch_array($res2))
{
$image=$row2['img_name'];
$photosarray[$count]=$image;
$count=$count+1;
}
echo'<td>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>';
foreach($photosarray as $photo){
echo '
<div class="carousel-item">
<img src="../photo/'.$photo.'" class="d-block w-100" alt="...">
</div>
';
}
echo '<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</td>';
echo '<p class="font-weight-bold">click the image to view in full size.</p>
</div>
</tbody>
';
or
<td>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$counter = 1;
while($row2 = mysql_fetch_array($res2)){
?>
<div class="carousel-item<?php if($counter <= 1){echo " active"; } ?>">
<img alt="First slide image" src="<?php echo '../photo/'.$row2['img_name']; ?>"/>
</div>
<?php
$counter++;
}
mysql_close($connection);
?>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
<ol class="carousel-indicators">
<li data-target="#carousel-example-captions" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-captions" data-slide-to="1"></li>
<li data-target="#carousel-example-captions" data-slide-to="2"></li>
</ol>
</div>
</div>
</td>
<p class="font-weight-bold">click the image to view in full size.</p>
</div>
I am using the advanced custom fields Wordpress plugin to create various slides for a slider. To display the slider I am using Bootstraps Carousel.
The body of the slider if functioning fine. I don't, however, know how to loop through, count the slides and print a carousel indicator to the page for each slide.
I currently have 3 hardcoded at the top of the slider.
<ul id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<li class="carousel-inner">
<?php
$c = 0;
$class = '';
while ( have_rows('slide') ) : the_row();
$c++;
if ( $c == 1 ){ $class = ' active';}
else{ $class=''; } ?>
<?php
$image = get_sub_field('image'); ?>
<div class="carousel-item <?php echo $class; ?> image" style="background: url('<?php echo $image; ?>') no-repeat; background-size: cover; background-position: left center;">
</div>
<?php
endwhile; ?>
</li> <!-- end li.image -->
</ul> <!-- end ul -->
I need to find a way to open the ordered list before the slider starts and close it when it ends. At the same time, I need to echo out its li elements for each slide.
<section id="banner">
<?php if( have_rows('slides') ) { ?>
<?php
$num = 0;
$active = 'active';
?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php while( have_rows('slides') ) : the_row() ; ?>
<li data-target="#carouselExampleIndicators" data-slide-to="<?php echo $num; ?>" class="<?php echo $active; ?>"></li>
<?php
$num++;
$active = '';
?>
<?php endwhile; ?>
</ol>
<div class="carousel-inner">
<?php $active = 'active'; ?>
<?php while( have_rows('slides') ) : the_row() ;
$image = get_sub_field('image');
$mainText = get_sub_field('main_text');
$subText = get_sub_field('sub_text');
?>
<div class="carousel-item <?php echo $active; ?>">
<img class="d-block w-100" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<div class="carousel-caption d-none d-md-block">
<h5><?php echo $mainText; ?></h5>
<p><?php echo $subText; ?></p>
</div>
</div>
<?php $active = ''; ?>
<?php endwhile; ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<?php } ?>
</section>
If your HTML is like this :
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img class="d-block img-fluid" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Then use this php code :
<?php
$sliders = get_field('slide');
if($sliders){ ?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php $isActive ='';
foreach($sliders as $key=>$slider){
if($key==0){
$isActive = 'active';
}
echo '<li data-target="#carouselExampleIndicators" data-slide-to="'.$key.'" class="'.$isActive.'"></li>';
} ?>
</ol>
<div class="carousel-inner" role="listbox">
<?php
$activeSlide ='';
foreach($sliders as $key=>$sliderimg){
if($key==0){
$activeSlide = 'active';
}
echo '<div class="carousel-item '.$activeSlide.'">';
echo '<img class="d-block img-fluid" src="'.$sliderimg['image']." alt="First slide">';
echo '</div>';
?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
<?php } ?>
I have to echo a blob image in a carousel but I am not able to use single quotes for the img tag line because of the base64_encode() can someone explain me how to use the double quotes for the img line. Thanks !!!!
Here is my code :
echo "
<div id=\"Carousel$i\" class=\"myCarousel carousel slideCarousel\" data-ride=\"carousel\" data-interval=\"5000\">
<ol class=\"carousel-indicators\">
<li data-target=\"#Carousel$i\" data-slide-to=\"0\" class=\"active\"></li>
<li data-target=\"#Carousel$i\" data-slide-to=\"1\"></li>
<li data-target=\"#Carousel$i\" data-slide-to=\"2\"></li>
<li data-target=\"#Carousel$i\" data-slide-to=\"3\"></li>
</ol>
<div class=\"carousel-inner\" role=\"listbox\">
<div class=\"item active\">";
// HEREEEEE !!!!!!
echo '<img src=\"data:image/jpeg;base64,'.base64_encode($image).'\"/>';
echo "</div>
<div class=\"item\">
<img class=\"slideimage\" src=\"images/diving3.jpg\" alt=\"Chania\">
</div>
<div class=\"item\">
<img class=\"slideimage\" src=\"images/Ambergris-Divers-PADI-Scuba-Course.jpg\" alt=\"Chania\">
</div>
</div>
<a class=\"left carousel-control\" href=\"#Carousel$i\" role=\"button\" data-slide=\"prev\">
<span class=\"glyphicon glyphicon-chevron-left\" aria-hidden=\"true\"></span>
<span class=\"sr-only\">Previous</span>
</a>
<a class=\"right carousel-control\" href=\"#Carousel$i\" role=\"button\" data-slide=\"next\">
<span class=\"glyphicon glyphicon-chevron-right\" aria-hidden=\"true\"></span>
<span class=\"sr-only\">Next</span>
</a>
</div>
";
This should work (stop using double quotes for echo-statements, just to avoid concatenating some strings.
echo '
<div id="Carousel'.$i.'" class="myCarousel carousel slideCarousel" data-ride="carousel" data-interval="5000">
<ol class="carousel-indicators">
<li data-target="#Carousel'.$i.'" data-slide-to="0" class="active"></li>
<li data-target="#Carousel'.$i.'" data-slide-to="1"></li>
<li data-target="#Carousel'.$i.'" data-slide-to="2"></li>
<li data-target="#Carousel'.$i.'" data-slide-to="3"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="data:image/jpeg; base64,'.base64_encode($image).'">
</div>
<div class="item">
<img class="slideimage" src="images/diving3.jpg" alt="Chania">
</div>
<div class="item">
<img class="slideimage" src="images/Ambergris-Divers-PADI-Scuba-Course.jpg" alt="Chania">
</div>
</div>
<a class="left carousel-control" href="#Carousel'.$i.'" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#Carousel'.$i.'" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
';