I currently trying to make it where My Database is storing image names like slider1.jpg and then I'm retrieving that from my database, but the issue is when I'm selecting * all from the table its only displaying the first link in the table. Any Ideas on how I can fix this in my code?
Heres my PHP:
<?php
//Gets Links
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
if($stmt->rowCount() > 0)
{
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
And Heres my Carousel Slider.
<header>
<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">
<!-- Slide One - Set the background image for this slide in the line below -->
<div class="carousel-item active" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>First Slide</h3>
<p>This is a description for the first slide.</p>
</div>
</div>
<!-- Slide Two - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Second Slide</h3>
<p>This is a description for the second slide.</p>
</div>
</div>
<!-- Slide Three - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Third Slide</h3>
<p>This is a description for the third slide.</p>
</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>
</header>
You need to utilize a loop on the results so you can build each slide with a new $row from the database query:
<?php
// build a clean array of slides from the db grab
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
$slides = [];
if($stmt->rowCount() > 0) {
$slides = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
.... starter html ....
<ol class="carousel-indicators">
<?php foreach($slides as $i => $slide) { ?>
<li data-target="#carouselExampleIndicators"
data-slide-to="<?php echo $i;?>"
class="<?php echo (!$i?'active':'');?>"></li>
<?php }?>
</ol>
.... more html ....
<div class="carousel-inner" role="listbox">
<?php foreach($slides as $i => $slide) { ?>
<div class="carousel-item <?php echo (!$i?'active':'');?>"
style="background-image: url('images/slider/<?php echo $slide['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3><?php echo $slide['slide_name'];?></h3>
<p><?php echo $slide['slide_desc'];?></p>
</div>
</div>
<?php } ?>
</div>
.... the rest of your html output ....
Related
This question already has answers here:
PHP with Bootstrap Carousel
(4 answers)
Closed 2 years ago.
i have a problem to do with Carousel Slider. i am adding an image and when i was listing in index.php. When i was adding and listing second image, it is adding just below as in pictures.
enter image description here
enter image description here
Slider.php
<?php
$veriler = $db->query("SELECT * FROM haberlerekle ORDER BY id DESC LIMIT 10",PDO::FETCH_ASSOC)->fetchAll();
?>
<div id="demo" class="carousel slide" data-ride="carousel">
<?php $count = count($veriler);?>
<ul class="carousel-indicators">
<?php
$i=0;
for ($i=0;$i<$count;$i++) { ?>
<li data-target="#demo" data-slide-to="<?php echo $i; ?>"></li>
<?php } ?>
</ul>
<div class="carousel-inner">
<?php
$j=0;
foreach ($veriler as $row) { ?>
<div class="item <?php if($j==0) { echo "active"; } ?>">
<div style="max-width:1100px; max-height:500px;">
<img src=" <?php echo $row["haberekle_konu"]; ?>" alt="Los Angeles">
</div>
<div class="carousel-caption">
<h3> <?php echo $row["haberekle_baslik"]; ?> </h3>
</div>
</div>
<?php $j++; } ?>
</div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
Codes below.
<div id="demo" class="carousel slide" data-ride="carousel">
<?php
$veriler = $db->query("SELECT * FROM haberlerekle",PDO::FETCH_ASSOC)->fetchAll();
foreach ($veriler as $row) { ?>
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<div style="max-width:1100px; max-height:500px;"> <?php echo $row['haberekle_konu']; ?> </div>
<div class="carousel-caption">
<h3> <?php echo $row['haberekle_baslik']; ?> </h3>
</div>
</div>
<?php } ?>
</div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
admin.php -- a part of admin.php -- i think, all of the admin.php unnecessary. if it is important, now i can share.
<form>
<input name="haberekle_baslik" size="100"></input><br><br>
Başlık <br>
<textarea name="editor1" cols="150" rows="30"></textarea><br>
<input type="submit" name="submit" value="Yayınla" style="margin-top:5px;">
Yeni Ekle
</form>
<?php
if (isset($_REQUEST['editor1']) && !empty($_REQUEST['editor1'])) {
$baslik = $_REQUEST['haberekle_baslik'];
$content = $_REQUEST['editor1'];
} else {
echo "Lütfen Metin Alanını Doldurunuz";
}
if (isset($_REQUEST['submit'])) {
if (isset($content) && !empty($content)) {
}
}
$haberekle = $db->prepare("INSERT INTO haberlerekle (haberekle_baslik, haberekle_konu) VALUES (:baslik, :konu)");
$haberekle->bindValue(':baslik', $baslik, PDO::PARAM_STR);
$haberekle->bindValue(':konu', $content, PDO::PARAM_STR);
$isle = $haberekle->execute();
if($isle) echo 'ok';
else echo 'no';
?>
See i added images
For image store i selected "BLOB" but it doesn't work again.enter image description here
enter image description here
Thanks in advance
Below code is working with logics. Note: I have changed classname carousel-item to item.I hope it will help.
<?php
$veriler = $db->query("SELECT * FROM haberlerekle ORDER BY id DESC LIMIT 10",PDO::FETCH_ASSOC)->fetchAll();
?>
<div id="demo" class="carousel slide" data-ride="carousel">
<?php $count = count($veriler);?>
<ul class="carousel-indicators">
<?PHP
$i=0;
for ($i=0;$i<$count;$i++) { ?>
<li data-target="#demo" data-slide-to="<?php echo $i; ?>"></li>
<?php } ?>
</ul>
<div class="carousel-inner">
<?php
$j=0;
foreach ($veriler as $row) { ?>
<div class="item <?php if($j==0) { echo "active"; } ?>">
<div style="max-width:1100px; max-height:500px;">
<img src=" <?php echo $row["haberekle_konu"]; ?>" alt="Los Angeles">
</div>
<div class="carousel-caption">
<h3> <?php echo $row["haberekle_baslik"]; ?> </h3>
</div>
</div>
<?php $j++; } ?>
</div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
Here is my Bootstrap ui that the data is fetched from the database. As you can see, the carousel is going down and cannot click next it just goes down. from the page.
1
please see the image above
data that I'm fetching are texts and not image
and here is my code
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Carousel indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<?php
$dataid;
$data = mysqli_query($con,"SELECT * FROM feedback");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo '
<div class="row ">';
while($row = mysqli_fetch_array($data)) {
echo '
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<div class="item carousel-item active">
<p class="testimonial">'. $row['comment'] .'</p>
<p class="overview"><b>Anonymous</b></p>
<div class="star-rating">
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-heart" ></i></li>
<li class="list-inline-item"><i class="fas fa-thumbtack" ></i></li>
<li class="list-inline-item"><i class="fas fa-archive"></i></li>
<li class="list-inline-item"><i class="fa fa-trash" ></i></li>
<li class="list-inline-item"><i class="fa fa-star-o"></i></li>
</ul>
</div>
</div>
</div>
';
}
}
?>
<!-- Carousel controls -->
<a class="carousel-control left carousel-control-prev" href="#myCarousel" data-slide="prev">
<i class="fa fa-angle-left"></i>
</a>
<a class="carousel-control right carousel-control-next" href="#myCarousel" data-slide="next">
<i class="fa fa-angle-right"></i>
</a>
</div>
</div>
The carousal structure is like
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Third slide">
</div>
</div>
</div>
so you need to repeat the carousel-item
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php while($row = mysqli_fetch_array($data)) { ?>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<?php } ?>
</div>
</div>
If you need to display the text in the Carousel that comes from database then just give some padding and background-color to the Image
<div class="carousel-item active">
<img src="<?php echo imageSrcFromDb; ?>" width="1100" height="500">
<div class="carousel-caption">
<h3><?php echo ; //Your Heading text ?></h3>
<p><?php echo ; //Some description ?></p>
</div>
</div>
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<?php
while($row = mysqli_fetch_array($data)) {
echo '
<div class="item carousel-item">
<p class="testimonial">'. $row['comment'] .'</p>
<p class="overview"><b>Anonymous</b></p>
<div class="star-rating">
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-heart" ></i></li>
<li class="list-inline-item"><i class="fas fa-thumbtack" ></i></li>
<li class="list-inline-item"><i class="fas fa-archive"></i></li>
<li class="list-inline-item"><i class="fa fa-trash" ></i></li>
<li class="list-inline-item"><i class="fa fa-star-o"></i></li>
</ul>
</div>
</div>
';
}
?>
</div>
the while loop should be inside the class carousel-inner .
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 made a dynamic bootstrap slider according the data fetched from database in php. Here I want to display 3 grid per slider, it works completely. But I just want to create a 3 grid in a single row. Here i got the grid in new row. What I have to do to get the 3 grid in a single row?
<div class="container content">
<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>
</ol>
<div class="col-lg-12">
<h1><center>Client Reviews</center></h1>
</div>
<div class="carousel-inner">
<?php
foreach($hello as $review){
?>
<?php if ($i % 3 == 0):?>
<div class="item<?php if ($is_active) echo ' active'?>">
<?php endif?>
<div class="row">
<div class="col-xs-12" style="margin:35px;">
<div class="col-md-4">
<div class="caption">
<p class="text-info lead adjust2"><?php echo $review\['testimonial'\];?></p>
<p><span class="glyphicon glyphicon-minus"></span> <?php echo $review\['name'\];?></p>
</div>
</div>
</div>
</div>
<?php if (($i+1) % 3 == 0 || $i == count($review)-1):?>
</div>
<?php endif?>
<?php
$i++;
if ($is_active) $is_active = false;
};
?>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
I think u open add before loop.
[![<div class="container content">
<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>
</ol>
<div class="col-lg-12">
<h1><center>Client Reviews</center></h1>
</div>
<div class="carousel-inner">
<div class="row">
<div class="col-xs-12" style="margin:35px;">
<?php
foreach($hello as $review){
?>
<?php if ($i % 3 == 0):?>
<div class="item<?php if ($is_active) echo ' active'?>">
<?php endif?>
<div class="col-md-4">
<div class="caption">
<p class="text-info lead adjust2"><?php echo $review\['testimonial'\];?></p>
<p><span class="glyphicon glyphicon-minus"></span> <?php echo $review\['name'\];?></p>
</div>
</div>
<?php if (($i+1) % 3 == 0 || $i == count($review)-1):?>
</div>
<?php endif?>
<?php
$i++;
if ($is_active) $is_active = false;
};
?>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
</div>
</div>][1]][1]
How to loop the data? All image are displaying at one slide other slides are empty.
<div class="container">
<br>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php foreach($banner as $data) { ?>
<div class="item active">
<img src="<?php echo base_url()?>uploads/banner/<?php echo $data['image']?>" alt="Chania" width="460" height="345">
</div>
<?php }?>
<div class="item">
<img src="<?php echo base_url()?>assets/image/c.jpg" alt="Chania" width="460" height="345">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" 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="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
How to loop the data? All image are displaying at one slide other slides are empty.
Use the below code.
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
$i = 0;
foreach($banner as $data) { ?>
<div class="item <?php echo $i==0 ? 'active' : ''; $i++; ?>">
<img src="<?php echo base_url()?>uploads/banner/<?php echo $data['image']?>" alt="Chania" width="460" height="345">
</div>
<?php }?>
</div>