-2

This is my code:

$sql = "SELECT * FROM notifications";
if($result = mysqli_query($link, $sql)) {
    if(mysqli_num_rows($result) > 0) {  
        while($row = mysqli_fetch_array($result)) {
            $notification = "
<a class='d-flex align-items-center dropdown-item' href=".$row["link"].">
<div class='dropdown-list-image mr-3'><i class='fas fa-bell'></i>
<div class='bg-success status-indicator'></div>
</div>
<div class='font-weight-bold'>
<div class='text-truncate'><span>".$row["omschrijving"].".</span></div>
<p class='small text-gray-500 mb-0'>".$row["klant"]." - ".$row["createdat"]."</p>
</div>
</a>    
";
        }
    }
}

and further in my page is:

echo "<h6 class='dropdown-header'>Notificaties</h6>
$notification"

I really want to have the output as a single variable for different reasons.

Now it's only outputting the first notification from the database.

Is it possible to do like: echo $notification for each row there is in the table?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tim Nijland
  • 65
  • 1
  • 8

2 Answers2

0

Concatenating the notifications probably will do it

$notification = '';
while($row = mysqli_fetch_array($result))
    $notification .="
        <a class='d-flex align-items-center dropdown-item' href=".$row["link"].">
        <div class='dropdown-list-image mr-3'><i class='fas fa-bell'></i>
        <div class='bg-success status-indicator'></div>
        </div>
        <div class='font-weight-bold'>
        <div class='text-truncate'><span>".$row["omschrijving"].".</span></div>
        <p class='small text-gray-500 mb-0'>".$row["klant"]." - ".$row["createdat"]."</p>
        </div>
        </a>   ";

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
-1

Looks like you're writing over the $notification variable every instance in your while loop. You should look at concatenating that variable. Additionally you want to be aware that HTML inside a PHP variable may not always play nice. I would also recommend using heredoc syntax. It would look something like this:

$notification .= <<<EOD
HTML goes here
EOD;