Load Div HTML code inside PHP with Condition - php

I am getting images from database and want display that images in slider using HTML and PHP code. I have HTML code for sample slider is like below
<div class="carousel-inner">
<div class="item active">
<img class="img-responsive" src="http://placehold.it/1200x600/555/000&text=One" alt="...">
<div class="carousel-caption">
One Image
</div>
</div>
<div class="item">
<img class="img-responsive" src="http://placehold.it/1200x600/fffccc/000&text=Two" alt="...">
<div class="carousel-caption">
Another Image
</div>
</div>
<div class="item">
<img class="img-responsive" src="http://placehold.it/1200x600/fcf00c/000&text=Three" alt="...">
<div class="carousel-caption">
Another Image
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
</div>
</div>
Its working fine but instead use Placeholder images, I want my images which I am getting from database like below PHP code
<?php
$i= 0;
if(!empty($images)){ foreach($images as $image){ $i++;
echo $image->title;
echo $image->file_name;
}
}
?>
I want for first image and then for all other images. I am new in PHP and so confused how I can do it, Let me know if any PHP expert can help me for do it.
Thanks!

It is pretty simple: you have to loop through the images and print the carousel div for each image inside the loop:
<?php
if (!empty($images)) {
?>
<div class="carousel-inner">
<?php
$active = 1;
foreach ($images as $image) {
?>
<div class="item <?php if ($active == 1) echo "active"; ?>">
<img class="img-responsive" src="<?php echo $image->title; ?>" alt="...">
<div class="carousel-caption">
<?php echo $image->file_name; ?>
</div>
</div>
<?php
$active++;
}
?>
</div>
<!-- Controls: Add them inside the condition: if(!empty($images) -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
<?php
}
?>

Related

How can i fill up a bootstrap carousel with images from database?

Trying to fill up a bootstrap carousel with multiple images from database,this is my code yet but doesnt seem to work. Im using PHP PDO,so got a query fetched before what my foreach based on.The query is fine cause without the carousel it lists the images as should be.The issue lays within these lines.
<?php
$stmt = $pdo->prepare("SELECT * FROM images");
$stmt->execute();
$image = $stmt->fetchAll();
?>
<?php foreach($image as $images)
{
?>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="/upload/<?php echo $images['image']?>" alt="First slide">
</div>
<div class="carousel-item">
<?php
}
?>
</div>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="false"></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="false"></span>
<span class="sr-only">Next</span>
</a>
Place <div class="carousel-inner"> outside the loop, you should only loop over
<div class="carousel-item active">
<img class="d-block w-100" src="/upload/<?php echo $images['image']?>" alt="First slide">
</div>
Example:
<?php
$stmt = $pdo->prepare("SELECT image FROM images");
$stmt->execute();
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php foreach ($images as $index => $image): ?>
<div class="carousel-item<?= !$index ? ' active' : '' ?>">
<img class="d-block w-100" src="<?= $image['image'] ?>" alt/>
</div>
<?php endforeach; ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

Error slider carousel Boostrap in codeIgniter

My website already connect with database, but somehow bootstrap carousel that I use for slider not working... What I expected the image will be like this:
< image > < image > < image >
But what I get is:
<image>
<image>
<image>
I don't know what's wrong with my code... Can somebody help me?
<!--- Slider -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php foreach ($slider as $slider): ?>
<div class="item active">
<img src="<?php echo base_url('upload/slider/'.$slider->image)?>">
<div class="carousel-caption">
<h3><?php echo $slider->name?></h3>
</div>
</div>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only"> previous</span>
</a>
<a class="right carousel-control" href="=#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only"> next </span>
</a>
</div>
I use this and work fine.
<div id="myCarousel" class="carousel slide" data-ride="carousel" >
<ol class="carousel-indicators">
<?php for($j=0; $j<count($slider); $j++) {?>
<li data-target="#myCarousel" data-slide-to="<?php echo $j; ?>" class=""></li>
<?php }; ?>
</ol>
<div class="carousel-inner" role="listbox" >
<?php $i=1;
foreach ($slider as $slider) { ?>
<div class="item <?php echo ($i==1) ? "active" : "" ?>">
<img src="<?php echo base_url('upload/slider/'.$slider->image) ?>" alt="" class="img-responsive" style="width:100%" title="">
<div class="carousel-caption">
<h3><?php echo $slider->name?></h3>
</div>
</div>
<?php $i++; } ?>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only"> previous</span>
</a>
<a class="right carousel-control" href="=#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only"> next </span>
</a>
<div class="carousel-line"></div>
</div>

Dynamic slide php bootstrap

I made dynamic carousel but if I include 'active-item' in loop it not working, this solution is silly but handle. How can I include 'active-item' properly?
<form class="form" action="routes.php" method="POST">
<div id="carouselExampleControls" class="carousel slide bg-light text-primary" data-ride="carousel">
<div class="carousel-inner"
<div class="carousel-item active" >
<img class="d-block m-auto " style="width: 800px; height:600px; " alt="There is <?php echo count($photos);?> photos">
</div>
<?php foreach ($photos as $photo) {?>
<div class="carousel-item">
<img class="d-block m-auto" style="width: 800px; height:600px;" src="../css/image/<?php echo $photo['pic_name']?>" alt="<?php echo $photo['pic_name']?>">
</div>
<?php }?>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</form>
Write a php code as below
$active = ($photo== 1)? 'active-itme':'';
and echo it in the class like
class='<?php echo $active?>'
So it will be triggered only on the first slider image. Since you need the first slider image to be active.
it's important to write 'item-active' properly, this is how I handle it, and its work
<div class="carousel-inner" >
<?php $i=1; foreach ($photos as $photo) {?>
<div class="carousel-item <?php if ($i==1)echo 'active'?>"><?php $i++;?>
<img class="d-block m-auto" style="width: 800px; height:600px;" src="../css/image/<?php echo $photo['pic_name']?>" alt="<?php echo $photo['pic_name']?>">
</div>
<?php }?>
</div>

Bootstrap 3 dynamic carousel doesnt slide

Please help. when i use the default carousel,
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
...
slide does work, but when i while looped the
<div class="item active">...</div> so the images will be dynamic, it doesnt slide anymore. instead, the images just stacked each other as seen here:
image here
Here is my code:
<!-- carousel -->
<div id="myCarousel" class="carousel slide " data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php
$x = new DB();
$y= $x->selectWithCount(0,'tblimagecarousel',null,null);
// var_dump($y);
$numofimages = $y->fetchColumn();
// echo $numofimages;
for($x=0;$x<$numofimages;$x++)
{
echo "<li data-target='#myCarousel' data-slide-to='{$x}'></li>";
}
?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
$x = new DB();
$y= $x->select(0,'*','tblimagecarousel',null,null);
while($row = $y->fetchObject()){
echo"
<div class='item active'>
<img src='../assets/images/".$row->img."' alt='".$row->title."' width='100%' height='345'>
<div class='carousel-caption'>
<h3>".$row->title."</h3>
<p>".$row->description."</p>
</div>
</div>";
}
?>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev" >
<span><img src="../assets/images/ico_prev.png" height="50px" style="margin-top:500%"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span><img src="../assets/images/ico_next.png" height="50px" style="margin-top:500%"></span>
<span class="sr-only">Next</span>
</a>
</div>
I looked at your code your issue is in your loop
the carousel can only have ONE item with the active class at a time (if you only want one to show)
this does not contain the php portion you will have to construct your loop to NOT add the active class (possibly add the active class to the 1st item after)
HTML
<div class="item">
<img src="../assets/images/image1.png" alt="Image one" width="100%" height="345">
<div class="carousel-caption">
<h3>Image one</h3>
<p>Image Description 1</p>
</div>
</div>
<div class="item">
<img src="../assets/images/image2.png" alt="Image two" width="100%" height="345">
<div class="carousel-caption">
<h3>Image two</h3>
<p>Image Description 2</p>
</div>
</div>
<div class="item">
<img src="../assets/images/image3.png" alt="Image three" width="100%" height="345">
<div class="carousel-caption">
<h3>Image three</h3>
<p>Image Description 3</p>
</div>
</div>
<div class="item">
<img src="../assets/images/image4.png" alt="Image four" width="100%" height="345">
<div class="carousel-caption">
<h3>Image four</h3>
<p>Image Description 4</p>
</div>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span><img src="../assets/images/ico_prev.png" height="50px" ></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span><img src="../assets/images/ico_next.png" height="50px" ></span>
<span class="sr-only">Next</span>
</a>
</div>
CSS
body{background-color:#ccc;}
.item{height:400px;}
js
$(document).ready(function(){
$('#myCarousel').carousel();
//this will add the active class to the first item
$('.carousel-inner div.item:first-of-type' ).addClass( "active" );
})
You can view a fiddle HERE

How to integrate PHP code into HTML Carousel code?

Can anybody help with the below please. Im having a bit of difficulty getting my head around how to get it to work.
The code is for a thumbnail carousel.
Here is the HTML
<div class="carousel slide" id="myCarousel">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item" data-slide-number="0">
<img src="http://placehold.it/770x300&text=one">
</div>
<div class="item" data-slide-number="1">
<img src="http://placehold.it/770x300&text=two">
</div>
<div class="item" data-slide-number="2">
<img src="http://placehold.it/770x300&text=three">
</div>
<div class="item" data-slide-number="3">
<img src="http://placehold.it/770x300&text=four">
</div>
<div class="item" data-slide-number="4">
<img src="http://placehold.it/770x300&text=five">
</div>
<div class="item" data-slide-number="5">
<img src="http://placehold.it/770x300&text=six">
</div>
</div>
<!-- Carousel nav -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
<!--thumbnails-->
<div class="row hidden-xs" id="slider-thumbs">
<ul class="hide-bullets">
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-0">
<img src="http://placehold.it/170x100&text=one">
</a>
</li>
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-1">
<img src="http://placehold.it/170x100&text=two">
</a>
</li>
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-2">
<img src="http://placehold.it/170x100&text=three">
</a>
</li>
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-3">
<img src="http://placehold.it/170x100&text=four">
</a>
</li>
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-4">
<img src="http://placehold.it/170x100&text=five">
</a>
</li>
<li class="col-sm-2">
<a class="thumbnail" id="carousel-selector-5">
<img src="http://placehold.it/170x100&text=six">
</a>
</li>
</ul>
</div>
The above code works fine, however I am trying to integrate the below PHP code so that the slider has dynamic images with thumbnails.
Here is the PHP code to display the images:
<?php
if ($images = get_field('images', $design_id)) {
foreach ($images as $image) {
echo '<img src="' . $image['image']['sizes']['large'] . '" />';
}
}
?>
If anyone could help or point me in the right direction, it would be appreciated!
Try this. Note where code starts and ends i didn't copy all of your block:
<div class="carousel-inner">
<?php
if ($images = get_field('images', $design_id)) {
foreach ($images as $key => $image) {
$active = $key == 0 ? 'active' : '';
echo '<div class="<?php echo $active;?> item" data-slide-number="<?php echo $key; ?>">';
echo '<img src="' . $image['image']['sizes']['large'] . '" />';
echo '</div>'; }
}
?>
</div>
Explanation. Showing the images is not only to print img src=..., so you have to see what html block to put in the foreach loop so it be showed as the html you posted. $key is the index that you need to mark the slides. $active shows which slide is active, so you need to set it only ones (in our case when $key is 0)
Cheers.

Categories