Jump to content

How to create a slideshow to show Database content using Bootstrap Carousel?


Recommended Posts

I've been working on developing a CMS blog and now I'm trying to create a slideshow wit Bootstrap Carousel on the homepage to present the dynamic content (images + text) using the data from table 'posts'.

I tested this code, and it only presents one post. I mean, It's not possible to go to the next slide. I want to show all the posts on the slides.

*The DB connection is already on the includes. The connection was written on a small file called DB.php

Home.php

<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">
              <?php
              // The default SQL query
                  $sql = "SELECT * FROM posts ORDER BY id desc";
                  $stmt = $ConnectingDB->query($sql);
                    while ($DataRows = $stmt->fetch()) {
                      $Id = $DataRows["id"];
                      $PostTitle = $DataRows["title"];
                      $Image = $DataRows["image"];
                      $PostText = $DataRows["post"];
              ?>
                    <!-- Slide -->
                    <div class="carousel-item active" style="background-image: url('uploads/<?php echo $Image; ?>')">
                      <div class="carousel-caption">
                        <div class="card-body black">
                          <h3 class="large-mistral-white"><?php echo $PostTitle; ?></h3>
                          <p class="small-times-white"><?php echo $PostText; ?></p>
                        </div>
                      </div>
                    </div>
                    <?php } ?>
                  </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>

DB.php

<?php

$DSN='mysql:host = localhost; dbname=everybody_blog';
$ConnectingDB = new PDO($DSN,'root','');
?>

 

Are you not getting the appropriate output?  If so add some test to validate what you are seeing.

$stmt = $ConnectingDB->query($sql);
print_r($stmt->fetchAll());

Or are your links not working?  Has to do with your JavaScript.

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.