How to integrate PHP code into HTML Carousel code? - php

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.

Related

Load Div HTML code inside PHP with Condition

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

Creating carousel using php and fetching data from sql

I am trying to create a page with carousel using php fetching image and title from sql. I created a carousel using html and css, but im not really sure how to transfer it using php.
My original carousel:
<div class="container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="item active">
<img src="images/aa1.jpg">
<div class="carousel-caption">
<h4><b>Title</h4></b>
<p><a class="label label-primary" href="news1.php" target="_blank" style="float: right;">Read More</a></p>
</div>
</div><!-- End Item -->
<div class="item">
<img src="images/aa2.jpg">
<div class="carousel-caption">
<h4><b>Title</h4></b>
<p><a class="label label-primary" href="news2.php" target="_blank" style="float: right;">Read More</a></p>
</div>
</div><!-- End Item -->
<div class="item">
<img src="images/aa3.jpg">
<div class="carousel-caption">
<h4><b>Title</h4></b>
<p><a class="label label-primary" href="news3.php" target="_blank" style="float: right;">Read More</a></p>
</div>
</div>
<div class="item">
<img src="images/aa4.jpg">
<div class="carousel-caption">
<h4><b>Title</h4></b>
<p><a class="label label-primary" href="news4.php" target="_blank" style="float: right;">Read More</a></p>
</div>
</div>
<div class="item">
<img src="images/aa5.jpg">
<div class="carousel-caption">
<h4><b>Title</h4></b>
<p><a class="label label-primary" href="news5.php" target="_blank" style="float: right;">Read More</a></p>
</div>
</div>
</div>
<ul class="list-group col-sm-4">
<li data-target="#myCarousel" data-slide-to="0" class="list-group-item active"><h5>Tilte Tilte Tilte</h5></li>
<li data-target="#myCarousel" data-slide-to="1" class="list-group-item"><h5>Tilte Tilte Tilte</h5></li>
<li data-target="#myCarousel" data-slide-to="2" class="list-group-item"><h5>Tilte Tilte Tilte</h5></li>
<li data-target="#myCarousel" data-slide-to="3" class="list-group-item"><h5>Tilte Tilte Tilte</h5></li>
<li data-target="#myCarousel" data-slide-to="4" class="list-group-item"><h5>Tilte Tilte Tilte</h5></li>
</ul>
<!-- Controls -->
<div class="carousel-controls">
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
Now I created a database named test with a table named carousel looks like:
----------------------------------------------
| title | image | link |
----------------------------------------------
| demo | demo.png |images/demo.png |
----------------------------------------------
I tried calling it but nothing works. I'm new to php.
My Code:
<?php
$link = mysqli_connect("localhost", "root", "", "test");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM news";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<div class=\"container\">";
echo "<div id=\"myCarousel\"class=\"carousel slide\"data-ride=\"carousel\">";
echo "<div class=\"carousel-inner\">";
echo "<div class=\"item active\">";
echo "<img alt=\"News\" src=\"images/{$row["image"]}\">";
echo "<p>{$row["title"]}</p>";
echo "<ul class=\"list-group col-sm-4\"href=\"{$row["link"]}\"echo "<b><h4>{$row["title"]}">;
<a data-slide="prev" href="#Carousel" class="left carousel-control">‹</a> <a data-slide="next" href="#Carousel" class="right carousel-control">›</a> </div>
echo "</div>"
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
How can I do that?
You have to add while loop in your HTML file, try using this.
<div id="slideshow">
<div id="slidesContainer">
<div class="slide">
<?php
error_reporting(E_ALL);
$query = "SELECT id, path, name FROM images";
$result = mysql_query($query);
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<img src=\"$row[path]\" title=\"$row[name]\" alt=\"\" />";
}
?>
</div>
</div>
</div>

Trying to make a bootstrap carousel slide dynamic with a database using php

The below html just shows the images one by one down the page and not in a slideshow. I've put the images in the database giving each one their own column. Is this wrong? How can I make the images be a slideshow. Thanks for helping.
<div id="lodgeGallery" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#lodgeGalleryl" data-slide-to="0" class="active"></li>
<li data-target="#lodgeGalleryl" data-slide-to="1"></li>
<li data-target="#lodgeGalleryl" data-slide-to="2"></li>
<li data-target="#lodgeGalleryl" data-slide-to="3"></li>
<li data-target="#lodgeGalleryl" data-slide-to="4"></li>
<li data-target="#lodgeGalleryl" data-slide-to="5"></li>
<li data-target="#lodgeGalleryl" data-slide-to="6"></li>
<li data-target="#lodgeGalleryl" data-slide-to="7"></li>
<li data-target="#lodgeGalleryl" data-slide-to="8"></li>
<li data-target="#lodgeGalleryl" data-slide-to="9"></li>
<li data-target="#lodgeGalleryl" data-slide-to="10"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<?php
//<img src="img/img1.jpg" alt="">
echo "<img src='img/$galleryImg1'";
?>
</div>
<div class="item">
<?php
//<img src="img/img2.JPG" alt="">
echo "<img src='img/$galleryImg2'";
?>
</div>
<div class="item">
<?php
//<img src="img/img3.png" alt="">
echo "<img src='img/$galleryImg3'";
?>
</div>
<div class="item">
<?php
//<img src="img/img4.png" alt="">
echo "<img src='img/$galleryImg4'";
?>
</div>
<div class="item">
<?php
//<img src="img/img5.jpg" alt="">
echo "<img src='img/$galleryImg5' ";
?>
</div>
<div class="item">
<?php
//<img src="img/img6.jpg" alt="">
echo "<img src='img/$galleryImg6' ";
?>
</div>
<div class="item">
<?php
//<img src="img/img7.jpg" alt="">
echo "<img src='img/$galleryImg7' ";
?>
</div>
<div class="item">
<?php
//<img src="img/img8.jpg" alt="">
echo "<img src='img/$galleryImg8'";
?>
</div>
<div class="item">
<?php
//<img src="img/img9.jpg" alt="">
echo "<img src='img/$galleryImg9' ";
?>
</div>
<div class="item">
<?php
//<img src="img/img10.JPG" alt="">
echo "<img src='img/$galleryImg10' ";
?>
</div>
<div class="item">
<?php
//<img src="img/img11.jpg" alt="">
echo "<img src='img/$galleryImg11' ";
?>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#lodgeGallery" 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="#lodgeGallery" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
Seeing your code, i think you have ne error in the syntax of how you echo the image from server.
It should be
"<img src ='img/'.$galleryImg1.'"
For pulling the images from a database:
2 columns: RowID & ImageURL
select RowID, Image URL from db
$sql = ("SELECT RowID,ImageURL FROM db.dbo.table");
$res = odbc_exec($dbConn,$sql);
while ($images = odbc_fetch_array($res)){
echo "<img src="img/".$images."'/>";
}

Boostrap thumbnail slider load dynamic conent

I have a thumbnail slider on index page
<div class="productslider carousel slide data-slider" id="prod158">
<div class="carousel-inner">
<div class="item active">
<ul>
<li class="product col-sm-2">
<a href="#" alt="">
<img src="#" width="160" height="120" alt="loading">
<span>Title</span>
</a>
<span class="text">Price</span>
</li>
</ul>
</div>
<a data-slide="prev" href="#prod158" class="left carousel-control">
<span class="prev_btn"></span>
</a>
<a data-slide="next" href="#prod158" class="right carousel-control">
<span class="next_btn"></span>
</a>
</div>
So how do I load dynamic data on this slider (where & how i pleased foreach loop).
Note :
This is a laravel project.if you use blade syntax,then i will more helpful.
li should be looped five times.
if there is 10 products then item class should be looped 2 times.
Data Source : Product::all();
<div class="productslider carousel slide data-slider" id="prod158">
<div class="carousel-inner">
#foreach(array_chunk($products,5) as $row)
<div class="item active">
<ul>
#foreach($row as $product)
<li class="product col-sm-2">
<a href="#" alt="">
<img src="#" width="160" height="120" alt="loading">
<span>Title</span>
</a>
<span class="text">Price</span>
</li>
#endforeach
</ul>
</div>
#endforeach
<a data-slide="prev" href="#prod158" class="left carousel-control">
<span class="prev_btn"></span></a>
<a data-slide="next" href="#prod158" class="right carousel-control">
<span class="next_btn"></span></a>

C# foreach within foreach carousel, display 4 item in each item

I have the following site,
http://bamboobeez.com/
how could i display 5 images instead of 3?
I cant seem to get the below code right?
<div id="slider-fixed-products" class="carousel slide">
<div class="carousel-inner">
<div class="active item">
<ul class="thumbnails">
<?$i=0;
foreach ($ads as $ad):?>
<?if ($i%3==0 AND $i!=3):?></ul></div><div class="item"><ul class="thumbnails"><?endif?>
<li class="span3">
<div class="thumbnail">
<a href="<?=Route::url('ad', array('category'=>$ad->category->seoname,'seotitle'=>$ad->seotitle))?>">
<?if($ad->get_first_image()!== NULL):?>
<img src="<?=URL::base('http')?><?=$ad->get_first_image()?>" >
<?else:?>
<img src="http://www.placehold.it/200x200&text=<?=$ad->category->name?>">
<?endif?>
</a>
<div class="caption">
<h5><?=$ad->title?></h5>
<p><?=substr(Text::removebbcode($ad->description), 0, 30)?></p>
</div>
</div>
</li>
<?$i++;
endforeach?>
</ul>
</div>
</div>
<a class="left carousel-control" href="#slider-fixed-products" data-slide="prev">‹</a>
<a class="right carousel-control" href="#slider-fixed-products" data-slide="next">›</a>
</div>
<?if ($i%5==0 AND $i!=5):?></ul></div><div class="item"><ul class="thumbnails">
in if condition use 5 instead of 3

Categories