Home > front end >  PHP loop echo ignores container div ( Bootstrap 5 )?
PHP loop echo ignores container div ( Bootstrap 5 )?

Time:11-29

This is the problem i am facing. I am echoing a container of cards in another loop. This is the first method i am using, in this method i am simply calling the card information from the database:

public function readNotes()
{

    $stmt = parent::connect()->prepare($this->getNotesQuery());
    $stmt->execute();

    if ($stmt->rowCount() == 0) {
        echo "<h2>No notes found! Start by adding one!</h2>";
    } else {
        while ($note = $stmt->fetch()) {
            echo "<div class='card' style='width: 18rem;'>
            <div class='card-body text-dark'>
                <h5 class='card-title'>{$note['note_title']}</h5>
                <p class='card-text'>{$note['note']}</p>
                <a href='#' class='card-link'>Card link</a>
                <a href='#' class='card-link'>Another link</a>
            </div>
        </div>";
        }
    }
}

This works as it should, then i have a second method where i call the readNotes() in a container:

public function readPostBook()
{
    $stmt = parent::connect()->prepare($this->sql);
    $stmt->execute();

    if ($stmt->rowCount() == 0) {
        echo "<h2>Post not found!</h2>";
    } else {
        while ($post = $stmt->fetch()) {
            echo "<div class='col d-flex flex-wrap gap-3 justify-content-center'>
                      {$this->readNotes()} //Cards are added, but the container is ignored
                  </div>"
            
            }
        }
    }
}

The cards are fetched as they should but when i inspect the page the container div doesn't exist. But when i add the cards directly without the readNotes() the container isn't ignored. What am i doing wrong?

CodePudding user response:

I did find the answer in this thread Echo function jumping outside of Div

You can use return instead of echo IF you have to return just 1 value. In my situation i had to echo more than one. you can read more on the post above!

  • Related