bootstrap 2.3 multiple images / items in carousel - php

I am working on a PHP based website which is using Twitter Bootstrap 2. I am pulling the users from MySQL database and trying to display them in multiple frames/items of carousel on a screen rather than a single item using while loop.
This is what my carousel looks like at present, as you will notice user 5 is not supposed to appear the way it is right now and is only supposed to appear when i click on arrow on right side of the carousel.
This is what my php code looks like
<div class="carousel slide" id="myCarousel">
<h4 class="carousel-title">Text title</h4>
<div class="carousel-inner">
<?php
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="item active">
<div class="span3 mef-3">
<div class="about portrait">
<div class="tooltip-demo">
<a href="#" rel="tooltip" data-placement="top" data-toggle="tooltip"
data-title="">
<img class="img-circle" width="180" height="200" data-src="holder.js/360x270"
alt="270x280"
style="" src="assets/img/adviser.png"></a>
</div>
<div class="caption">
<h3 class="name" style="text-align: center"><?php echo $row['fname']." ".$row['lname'] ?></h3>
<p style="text-align: center">
Specialities</p>
</div>
<div class="mefradio">
<input type="radio" name="adviser" id="adviser" value='<?php echo $row['user_id']."|".$row['fname']." ".$row['lname'] ?>'><br>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<a data-slide="prev" href="#textCarousel" class="left carousel-control">‹</a>
<a data-slide="next" href="#textCarousel" class="right carousel-control">›</a>
</div>
I will really appreciate any guidance on how to get it to work
UPDATE
This is the updated code that is working, thanks to #I can Has Kittenz
<div class="carousel slide" id="myCarousel">
<h4 class="carousel-title">Text title</h4>
<div class="carousel-inner">
<?php
$i = 1;
$next_item = true;
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
if ($i == 1) {
echo '<div class="item active">';
} elseif ($next_item == true) {
echo '<div class="item">';
}
?>
<div class="span3 mef-3">
<div class="about portrait">
<div class="tooltip-demo">
<a href="#" rel="tooltip" data-placement="top" data-toggle="tooltip"
data-title="">
<img class="img-circle" width="180" height="200" data-src="holder.js/360x270"
alt="270x280"
style="" src="assets/img/adviser.png"></a>
</div>
<div class="caption">
<h3 class="name"
style="text-align: center"><?php echo $row['fname'] . " " . $row['lname'] ?></h3>
<p style="text-align: center">
Specialities</p>
</div>
<div class="mefradio">
<input type="radio" name="adviser" id="adviser"
value='<?php echo $row['user_id'] . "|" . $row['fname'] . " " . $row['lname'] ?>'><br>
</div>
</div>
</div>
<?php
$next_item = false;
if ($i % 4 == 0) {
echo '</div>';
$next_item = true;
}
$i++;
}
?>
</div>
<a data-slide="prev" href="#myCarousel" class="left carousel-control">‹</a>
<a data-slide="next" href="#myCarousel" class="right carousel-control">›</a>
</div>
The JS that needs to go with this is as following
<script>
$(document).ready(function(){
$('#myCarousel').carousel({
pause: true,
interval: false
});
});
$('#myCarousel').on('slid', '', function() {
var $this = $(this);
$this.find('.carousel-control').show();
if($('.carousel-inner .item:first').hasClass('active')) {
$this.find('.left.carousel-control').hide();
} else if($('.carousel-inner .item:last').hasClass('active')) {
$this.find('.right.carousel-control').hide();
}
});
</script>

Updated:
As per the way Bootstrap 2.3 carousel works, multiple items like the way you have done won't show 4 items in a row and only one .item class can have an .active class to it, so here's what we would do:
<div class="item active">
<div class="span3"><img></div>
<div class="span3"><img></div>
<div class="span3"><img></div>
<div class="span3"><img></div>
</div>
<div class="item">
<div class="span3"><img></div>
<div class="span3"><img></div>
<div class="span3"><img></div>
<div class="span3"><img></div>
</div>
That's how we are going to structure your elements to look like. So code in:
<!-- code follows -->
<div class="carousel-inner">
<?php
$i = 1;
$next_item = true;
while ($i < 10)
{
if ($i == 1)
{
echo '<div class="item active">';
}
elseif ($next_item == true)
{
echo '<div class="item">';
}
?>
<div class="span3 mef-3">
<!-- code follows -->
</div>
<?php
$next_item = false;
if ($i % 4 == 0)
{
echo '</div>';
$next_item = true;
}
$i++;
}
?>
Also, since you name your carousel as id=myCarousel, your prev and next button's href should be href="#myCarousel" and not href="#textCarousel".
Here's a demo of what it looks like.

Related

Print $variable value using jQuery is not working properly?

I have a small interface Site (check site interface once) where I am showing articles count in yellow box I am trying to print the value with help of jQuery in the column but value is printing 1 column after every yellow box
I have tried a lot but nothing works
<div class="news-card NewsList">
<div class="row">
<div class="col-md-2 ">
<div class="news-left pb-0">
<div class="view-more">
<a data-toggle="collapse" class="listSimilarNews" href="#collapse<?=$i?>" role="button" aria-expanded="true" aria-controls="#collapse<?=$r?>">
<span id="listSimilarNews"><?php echo (count($nestedarr)); ?></span><br>
Articles
<i class="accordionarrow icofont-rounded-down"></i>
</a>
</div>
</div>
</div>
</div>
</div>
$('#listSimilarNews').html( <?php echo (count($nestedarr)); ?>);
for more reference complete code with PHP
<?php
$mainarr = [];
for($i=0; $i < count($row); $i++){
?>
<div class="news-card NewsList">
<div class="row">
<div class="col-md-2 ">
<div class="news-left pb-0">
<div class="view-more"><a data-toggle="collapse" class="listSimilarNews" href="#collapse<?=$i?>" role="button" aria-expanded="true" aria-controls="#collapse<?=$r?>">
<span id ="listSimilarNews"><?php print_r(count($nestedarr)) ; ?>
</span><br>Articles <i class="accordionarrow icofont-rounded-down"></i></a>
</div>
</div>
</div>
</div>
</div>
<?php
$nestedarr=[];
while(($j) < count($row) && ($id == (string) $row[$j]['_id'])){
$i++;
$j++;
array_push($nestedarr, $row[$j]['newsSources']);
}
array_push($arr, $nestedarr);
?>
<script language='javascript'>
$('#listSimilarNews').html( '<?php echo (count($nestedarr)); ?>');
</script>
<?php
}
?>

Dynamic div close in php while loop

code i have written below is working fine but at the end of the looping the div is not closed its still opening a loop
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error());
$i=0; $split=0;
while($projects=mysql_fetch_array($recent_projects_conn)) {
$i++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
$split++;
if ($split % 4 == 0){
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
The Div has splited very well but in end of the loop div has not been closed. Thats only the problem please provide me the help to sort out the problem
When I inspect the element the last loop will show at the given result as follows:
<div class="col-sm-3">
<div class="col-item">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/1557301934.jpg" alt="User one">
</div>
<div class="info">
<div class="name">UPHOLSTERY</div>
<div class="degination">UPHOLSTERY</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div></div><div class="item"><div class="row">
I want to remove the two opening div's as dynamically. How can i set this to remove opened div's at then end of the looping
I just took a quick look and it looks like you are not closing the "carousel-inner" div
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql = "SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn = mysql_query( $recent_projects_sql ) or die( mysql_error() );
$i = 0;
$split = 0;
while ( $projects = mysql_fetch_array( $recent_projects_conn ) ) {
$i ++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>"
alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php $split ++;
if ( $split % 4 == 0 ) {
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
Add a Boolean check for the execution of loop, such as $check = true;, add this within the loop.
after the loop add this
if($check){
echo " </div></div>";
}
That's because at the end of iteration (in case of mod 4 and even without it), you keep 2 divs opened
echo '</div></div><div class="item"><div class="row">';

add additional echo in if statement

I have this following snippet:
<?php
if($people->num_rows >= 1) {
while($person = $people->fetch_object()) {
echo '
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card card-user">
<div class="content">
<div class="author">
<a href="'.$system->getDomain()?>/user/<?=$person->id.'">
<img class="avatar" src="'.$system->getProfilePicture($person).'">
<h4 class="title">'.$system->getFirstName($person->full_name).', '.$person->age.'</h4>
</a>
</div>
<p class="text-center text-muted">
';
echo $person->city.$system->ifComma($person->city); echo ' '.$person->country.'
</p>
</div>
</div>
</div>
';
}
} else {
?>
however next to the actual age (after the closing H4 tag) I'd like to add a further function which shows the current online status of the user.
The code for this would be as follows:
<?php if($system->isOnline($profile->last_active)) { echo '<i class="online-status online"></i>'; } else { echo '<i class="online-status offline"></i>'; } ?>
What is the best way to combine this snippet (online status) with the statement above?
Some expert help would be greatly appreciated
Separate your HTML and PHP code and do it like the below. Your requirement is done using a single line ternary operator.
<?php
if($people->num_rows >= 1) {
while($person = $people->fetch_object()) { ?>
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card card-user">
<div class="content">
<div class="author">
<a href="<?php echo $system->getDomain().'/user/'.$person->id;?>">
<img class="avatar" src="<?php echo $system->getProfilePicture($person);?>">
<h4 class="title"><?php echo $system->getFirstName($person->full_name).', '.$person->age;?></h4>
<?php echo ($system->isOnline($profile->last_active)) ? '<i class="online-status online"></i>' : '<i class="online-status offline"></i>'; ?>
</a>
</div>
<p class="text-center text-muted">
<?php echo $person->city.$system->ifComma($person->city); echo ' '.$person->country;?>
</p>
</div>
</div>
</div>
<?php
}
}
?>

How show results in another column via "Foreach"?

I want to display more results in another column via foreach from the database but I can't get it to work.
<div class="row">
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
</div>
As you can see, it's two different columns. I want to do foreach and show one set of information in one column and the other set in another. However, the code below returns the same value in both columns.
how can I do this? Thanks.
You are iterating through each item of results in both loops, so their results are the same. Why not try something like this
<?php
$cols = 3;
?>
<div class="row">
<?php
if (is_array($results))
{
for($i = 0 ; $i < $cols ; $i++)
{
echo "
<div class='col-md-".(12/$cols)."'>";
for ($j = 0 ; $j < (count($results) / $cols) + 1 ; $j++)
{
if ($i * $cols + $j < count($results)) // make sure it won't give errors for array out of range....
{
echo "
<div class='user-item'>
<div class='user-btns'>
<a href='#' class='btn btn-green btn-xs'><i class='fa fa-pencil'></i></a>
<a href='#' class='btn btn-red btn-xs'><i class='fa fa-times'></i></a>
</div>
<img src='".$results[$i* $cols + $j]->avatar."' alt=' class='img-responsive'/>
<div class='u-details'>
<h5><i class='fa fa-user'></i>".$results[$i* $cols + $j]->first_name . ' ' . $results[$i* $cols + $j]->last_name."</h5>
<h5><i class='fa fa-envelope'></i>".$results[$i* $cols + $j]->email."</h5>
<h5><i class='fa fa-user-md'></i>".$results[$i * $cols + $j]->username."</h5>
</div>
<div class='clearfix'></div>";
}
}
echo "
</div>";
}
}
?>
</div>

Twiiter Bootstrap carousel making dynamic with PHP

I am trying to make a carousel that uses Twitter Bootstrap default JavaScript and CSS but the images should be fetched from the MySQL database.
The HTML for the carousel that i am trying to make dynamic with php is like this:
<div class="carousel-inner">
<div class="active item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
The end result for PHP file that i'm expecting is like this
<div class="carousel-inner">
<?php renderAds(); ?>
</div>
The PHP function that i wrote is like this :
function renderAds(){
$query = queryAds();
$query_exe = mysql_query($query);
if(mysql_num_rows($query_exe) == 0){
echo '<img src="images/sampleAd.jpg" >';
}else{
$numb =1;
$flag =1;
while($fetched_data = mysql_fetch_array($query_exe)){
if($numb == 1){
if($flag == 1){
echo '<div class="active item">';
}else{
echo '<div class="item">';
}
}elseif($numb == 4){
echo '';
echo '</div>';
$numb =0;
}else{
echo '';
}
$numb++;
$flag++;
}
echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>';
echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>';
}
}
Here, by differing from default carousel of twitter bootstrap, i made each slide with 4 images, i expect that each slide transition contains 4 images instead of as by default the single image for one slide. In HTML it works but when I replace it with my PHP function first slide works but if i press next arrow in carousel whole div collapses.
How can I make it work ?
Note : Here Last image must contain the class marginLast, and the
other should have marginTop and pull-left classes.
What I expect to be rendered in html is like this:
<div class="carousel-inner">
<div class="active item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<div class="item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<div class="item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
How can i make my PHP function to display result markup like above?
Edit : The Output i get is like this:
<div id="myCarousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div><!-- end carousel-inner -->
</div><!-- myCarousel -->
According to markup this should work but it doesn't, the arrow by default if there is no more slide than one should be disabled but it is clickable.
Have you MySQL query return just an array. That way, your query's reusable (could be then made into a function) and you can render the data however you see fit.
<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
$sql = "SELECT * FROM table";
$res = $mysqli->query($sql);
$rows = array();
while ($row = $res->fetch_assoc()) {
$rows[] = $row;
}
?>
<div class="carousel">
<div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($rows as $row): ?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<a href="<?php echo $row['url']; ?>">
<img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>" />
</a>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
</div>
You'll obviously need to change the database query, as well as the values then used in the foreach loop. I've also used the mysqli functions, as the mysql functions are now deprecated in favour of MySQLi (MySQL improved).
I think i came out with answer for my own question, even though all the above answers gave me many ideas. Thank you all
function renderAds(){
$query = queryAds();
$query_exe = mysql_query($query);
if(mysql_num_rows($query_exe) == 0){
echo '<img src="images/sampleAd.jpg" >';
}else{
$numb =1;
$flag =1;
echo '<div class="carousel-inner">';
while($fetched_data = mysql_fetch_array($query_exe)){
if($numb == 1){
if($flag == 1){
echo '<div class="active item">';
}else{
echo '<div class="item">';
}
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
}elseif($numb == 4){
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginLast" ><img src="'.$fetched_data['image_url'].'"></a>';
$numb = 0;
echo '</div>';
}else{
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
}
$numb++;
$flag++;
}
if($numb > 1 && $numb < 4 ){
echo '</div>';
echo '</div>';
}elseif($numb == 1){
echo '</div>';
}elseif($numb == 4){
echo '</div>';
echo '</div>';
}
echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>';
echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>';
}
}
<div class="carousel-inner">
<?php
// Twitter Bootstrap Carousel.
$caseStudies = get_posts(array(
'numberposts' => 12,
'post_type' => 'case-study',
'meta_key' => 'show_case_study_home',
'meta_value' => 'yes',
'post_status' => 'publish',
));
$open = 0;
$close = 1;
$active = 0;
$didClose = false;
foreach($caseStudies as $caseStudy){
$image = get_field('image_home', $caseStudy->ID);
$header = get_field('header_home', $caseStudy->ID);
$intro = get_field('intro_home', $caseStudy->ID);
$activeCss = "";
if(($open)%4 == 0) {
if ($active == 0) $activeCss = " active";
echo '<div class="item'. $activeCss .'"><ul class="thumbnails">';
}
?>
<li class="span3">
<div class="case-study">
<img src="<?php echo $image['url']; ?>" alt="">
Read More
<h4><?php echo $header; ?></h4>
<p><?php echo $intro; ?></p>
</div>
</li>
<?
if(($close)%4 == 0) {
echo "<ul></div>";
$didClose = true; // Maybe the carousel has less than 4 items ...
}
$open++;
$close++;
$active++;
}
if($didClose == false) echo "</div></div>"; // If has less than 4 items in the carousel close the tags.
?>
</div><!--/.caroussel-inner -->
I solved it as :
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php $slider = $guest->getAllData("slider"); ?>
<?php foreach( $slider as $slide) : ?>
<li data-target="#myCarousel" data-slide-to="<?=$slide['id']; ?>" class=""></li>
<?php endforeach; ?>
</ol>
<div class="carousel-inner">
<?php foreach($slider as $slide) : ?>
<div class="item ">
<img src="images/slider/<?=$slide['img']; ?>" alt="Los Angeles">
</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>
and add JS code:
$('.carousel-indicators>li:first-child').addClass('active');
$('.carousel-inner>div:first-child').addClass('active');

Categories