Bootstrap OwlCarousel fetch data php - php

I have this slider that I want to show data from database it's fetch data but the Carousel style don't work.
I want to show the image and the title in Carousel so that ever time the admin change the images from database it's change in the web.
index.php
<!-- -->
<?php include('./includes/header.php');
require("conn.php");
$sql_query = "SELECT * FROM slider_tbl";
$result = $conn->query($sql_query);
?>
<!-- -->
<div class="container">
<br>
<div id="myslider" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myslider" data-slide-to="0" class="active"></li>
<li data-target="#myslider" data-slide-to="1"></li>
<li data-target="#myslider" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<?php
if($result->num_rows>0) {
while($row=$result->fetch_assoc()) {
?>
<div class="carousel-item active">
<img src="<?php echo $row['slider_imgpath']; ?>"
alt="<?php echo $row['img_title']; ?>"
style='width:100%'>
</div>
</div>
<?php
}
}
?>
<a class="carousel-control-prev" href="#myslider" 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="#myslider" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<!-- -->
<?php include('./includes/footer.php'); ?>
<!-- -->
conn.php
<?php
$user_name = "root";
$host_name = "localhost";
$password = "";
$db_name = "onlines";
$conn = new mysqli($host_name,$user_name,$password,$db_name);
if($conn->connect_error) {
die("Connnection Error Failed : ".$conn->connect_error);
}
else{
}
?>
the Carousel code work well if i dont use php but when i use it everthing stop in OwlCarousel .

Related

Adding image in Carousel Slider [duplicate]

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>

How to make carousel work properly with php

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

Issue while trying to display images from display on carousel slider

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 ....

Integrating Bootstrap Carousel Indicators with ACF Plugin

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 } ?>

create a slideshow of images inside bootstrap modal dynamically using PHP

<div class="modal-body">
<?php
$id=$_GET['id'];
$qry="select rel_movies from released_movies where rel_id='$id' ";
$qryr=$con->query($qry);
while($rr=$qryr->fetch_assoc()){
$film=$rr['rel_movies'];
$q="select * from gallery where category='$film'";
$qr=$con->query($q);
while($r=$qr->fetch_assoc()){
?>
<ol class="carousel-indicators">
<li data-target="#lightbox" data-slide-to="0" class="active"></li>
<li data-target="#lightbox" data-slide-to="1"></li>
<li data-target="#lightbox" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img src="../AbaamAdmin/uploads/<?php echo $r['images'];?>" width="900px" height="500px" >
</div> <!-- /.item active-->
</div> <!-- /.carousel-inner -->
<a class="left carousel-control" href="#lightbox" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
<a class="right carousel-control" href="#lightbox" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
<?php }} ?>
</div><!-- /.modal-body -->
I am trying to display images from database inside bootstrap modal as a slideshow. But after executed the above code, what I got is, all images appeared inside the modal, but both left and right icons are not working instead of that images are viewed with a y scroll.
I cannot figure out the error.
#Ashwini Agarwal solution is partial and to show both image indicators and images it cann't be done like that because can't run the while loop twice so the working solution will be to create arrays before loop, load fetched data into arrays and then use foreach function for both indicators and to show images also handle the active class with counter
PHP code
<?php
$id=$_GET['id'];
$qry="select rel_movies from released_movies where rel_id='$id' ";
$qryr=$con->query($qry);
while($rr=$qryr->fetch_assoc()){
$film=$rr['rel_movies'];
$q="select * from gallery where category='$film'";
$qr=$con->query($q);
$rows = array(); //Declare rows as arrays before loop
while($r=$qr->fetch_assoc()){ //Run Loop
$rows[] = $r; //Load Data in arrays
} //close Loop
} //close First Loop, Side Note, You don't need This Loop
?>
Now the Carousel inside Modal Body will look like this (explained with comments to understand how this is working)
<div class="modal-body">
<div id="lightbox" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php
$i = 1; //Counter
foreach ($rows as $r): //Foreach
$ol_class = ($i == 1) ? 'active' : ''; //Set class active for only indicator which belongs to respective Image
?>
//Here I add the counter to data-slide attribute and add class to indicator
<li data-target="#lightbox" data-slide-to="<?php echo $i;?>" class="<?php echo $ol_class; ?>"></li>
<?php $i++; ?>
<?php endforeach; ?> //Close Foreach
</ol>
<div class="carousel-inner">
<?php
$i = 1; //Counter
foreach ($rows as $r): //Foreach
$item_class = ($i == 1) ? 'item active' : 'item'; //Set class active for image which is showing
?>
<div class="<?php echo $item_class; ?>"> // Define Active Class Here
<img src="../AbaamAdmin/uploads/<?php echo $r['images'];?>" width="900px" height="500px" >
</div>
<?php $i++; ?>
<?php endforeach; ?> // Close Foreach
</div>
<a class="left carousel-control" href="#lightbox" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
<a class="right carousel-control" href="#lightbox" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div>
</div>
Put the carousel outside the loop.
<ol class="carousel-indicators">
<li data-target="#lightbox" data-slide-to="0" class="active"></li>
<li data-target="#lightbox" data-slide-to="1"></li>
<li data-target="#lightbox" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<?php $counter = 1; ?>
<?php while($r=$qr->fetch_assoc()) { ?>
<div class="item <?php echo ($counter++ == 1) ? 'active' : '' ?>">
<img src="../AbaamAdmin/uploads/<?php echo $r['images'];?>" width="900px" height="500px" >
</div> <!-- /.item active-->
<?php } ?>
</div> <!-- /.carousel-inner -->
<a class="left carousel-control" href="#lightbox" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#lightbox" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>

Categories