My Carousel does not pass the bootstrap images

Asked

Viewed 470 times

1

The code below is being used to show images that are in the BD, however it is not passing the photos:

<div id="conteudo2" class="row">
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" class="col-sm-12 col-md-6">
    <div class="carousel-inner">
<!--    <div class="carousel-item active btn-add-more">
-->     <!-- <i class="material-icons" for="uploader-file">add_photo_alternate</i> -->
<!--        <h1 style="cursor: default;">Adicionar mais imagens</h1>
--> <!-- <label class="btn btn-primary" for="uploader-file" style="margin-top: 15px;">
            Adicionar
        </label> -->
    <?php 
    $imagemApicultor = null;
    try {
        $prepared = $conexao_pdo->prepare("SELECT `imagem`, `titulo`, `descricao` FROM carousel_images WHERE `cod_apicultor` = :cod_apicultor order by cod desc");
        $codApicultor = $_SESSION["token"];
        $prepared->bindParam(":cod_apicultor", $codApicultor);
        $prepared->execute();

        if($prepared->rowCount() > 0) {
            $data = $prepared->fetchAll();
            for($i = 0; $i<sizeof($data); $i++) {
                ?>
                <div class="carousel-item active">
                    <div class="localdeimagem" style="width:100%;height:391px;background-position:center;background-size: cover;background-image: url('<?php echo 'api/'.$data[$i]["imagem"]; ?>');">
                    </div>
                    <div class="carousel-caption d-none d-md-block">
                        <h5><?php echo $data[$i]["titulo"] ?></h5>
                        <p><?php echo $data[$i]["descricao"] ?></p>
                    </div>

                </div>
                <?php
            }
        }


    } catch(PDOException $e) {
        print $e->getMessages();
        die();
    }
    ?>
</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>
  • Which version of bootstrap? This carousel is the original in bootstrap or you used another model?

  • version 4.1.3. It is his original version

  • I think it has something to do with "active," but I can’t identify the problem

1 answer

1


In your code you are placing the css class active on all carousel items. Try changing:

Of:

<div class="carousel-item active">

To:

<div class="carousel-item <?php print ($i == 0) ? 'active' : ''; ?>">

The above code puts the css class active only on the first carousel item.

Edit:

In a local example I separated php logic from HTML to increase readability and applied to the html structure example of bootstrap website with the classes of your example.

The final code went like this:

// esse trecho ficou no topo do arquivo do carossel
session_start();

try {
    $conexao_pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $conexao_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $prepared       = $conexao_pdo->prepare("SELECT `imagem`, `titulo`, `descricao` FROM carousel_images WHERE `cod_apicultor` = :cod_apicultor order by cod desc");
    $codApicultor   = $_SESSION["token"];

    $prepared->bindParam(":cod_apicultor", $codApicultor);
    $prepared->execute();

    $data = $prepared->fetchAll(PDO::FETCH_OBJ);
    $count = count($data);

} catch(PDOException $e) {
    print $e->getMessage();
    die();
}
<!-- Trecho do carossel -->
<div class="row">
    <div class="bd-example w-100">
        <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
            <div class="carousel-inner">
                <?php for ($i = 0; $i < $count; $i++) : ?>
                    <div class="carousel-item <?php print ($i == 0) ? 'active' : ''; ?>">
                        <div class="localdeimagem d-block" style="height:391px;background: url('<?php echo 'api/'.$data[$i]->imagem; ?>') no-repeat center center / cover;"></div>
                        <div class="carousel-caption d-none d-md-block">
                            <h5><?php print $data[$i]->titulo; ?></h5>
                            <p><?php print $data[$i]->descricao; ?></p>
                        </div>
                    </div>
                <?php endfor; ?>
                <a class="carousel-control-prev" href="#carouselExampleCaptions" 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="#carouselExampleCaptions" role="button" data-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
    </div>
</div>

And the carousel was passing automatically and the buttons "Next" and "Previous" also continued working.

  • I believe that using the class "active" in the first item of the "Indicators" and inside the first item of the "item" carousel is already right.

  • Thank you guys! It worked!!

  • But now you are not passing the photos alone. Does anyone know?

  • @Nathi I updated the answer with a functional example

  • Thanks!! It worked. Now I need to put this code if you do not have any image registered in the bank. I put it on Else, but it is completely deformed <div class="Carousel-Inner"> <div class="Carousel-item active btn-add-more"> <i class="material-icons" for="Uploader-file">add_photo_alternate</i> <H1 style="cursor: default;">Add more images</H1> <label class="btn btn-Primary" for="Uploader-file" style="margin-top: 15px;">Add</label>

  • @Nathi If my answer has solved your initial problem it is very important that you accept it as the answer to your question and if you are having trouble with something else you create a new question here in the OS so that I or someone else can help you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.