0

I would like to insert masonry into a php form that retrieves images from a MySQL database. I can retrieve the images, but when I try to put in masonry, the whole thing doesn't work at all.

Here is my code

$result = mysqli_query($con,"SELECT * FROM img");

while($row = mysqli_fetch_array($result)) {
  echo "<script src="/path/to/masonry.pkgd.min.js"></script>"
  echo "<div id="container">"
  echo "<img class='item' src='" . $row['path'] . "' />";
  echo "</div>"
}

mysqli_close($con);
?> 
user3435505
  • 157
  • 2
  • 10

1 Answers1

0

You have 3 issues with that code:

First of all, you don't really have to include your script for every new <div>. Just include your script once in the <head> section, as follows:

<html>
    <head>
        <script src="/path/to/masonry" />
        ...
    </head>

Secondly, you need to differentiate between " as a string container in your PHP code and as a character that should appear in your final HTML code. When using it as the latter, you need to escape it by adding a backslash (like so: \"), or simply replace it with an apostrophe:

$result = mysqli_query($con,"SELECT * FROM img");

while($row = mysqli_fetch_array($result)) {
  echo "<div id='container'>";
  echo "<img class='item' src='" . $row['path'] . "' />";
  echo "</div>";
}

mysqli_close($con);
?> 

And finally, as presented in the example, you forgot the semicolon terminator at the end of your lines of code inside the while loop.

mittelmania
  • 3,393
  • 4
  • 23
  • 48