I have a dynamic website that will have a list of artists in the form of cards, how do I load the database entries from MySQL so that they'd load exactly in this format and then auto load as i scroll? do i have to copy the container and reference the data with PHP in every single container?
<div id="feed">
<div class="feed_item">
<div class="feed_img"></div>
<h2 class="feed_title">Karamantoso</h2>
</div>
<div class="feed_item">
<div class="feed_img"></div>
<h2 class="feed_title">Yamyom</h2>
</div>
<div class="feed_item">
<div class="feed_img"></div>
<h2 class="feed_title">Hayom</h2>
</div>
<div class="feed_item">
<div class="feed_img"></div>
<h2 class="feed_title">Sha</h2>
</div>
</div>
As Pedro already said, I suggest you should first take a look at loading all of them at once before you have them on demand.
I can't really tell how far you progressed in PHP yet so I will just start explianing from where you process the query and store it in $result
.
First of all you don't have to prepare the HTML before hand because that doesn't work for a dynamic amount of entries, so we want to avoid hard coding wherever possible. Instead we let PHP do that for us.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//This gets executed for every MySQL entry
}
}
Inside the while loop we can use just echo
the entries to the HTML. The content for that we can get from $row
:
while($row = $result->fetch_assoc()) {
//This gets executed for every MySQL entry
echo "<div class=\"feed_item\">";
echo "<img src=\"" . $row['img'] . "\">";
echo "<h2 class=\"feed_title\">" . $row['title'] . "</h2>";
echo "</div>";
}
Keep in mind that for $row['whatever']
you have to of course use your own rows you declared in your MySQL table.
All of that php stuff should go into your feed
div:
<div id="feed">
<?
//Right here :)
?php>
</div>
So once you completely understood this, to load content on demand you'd probably have to use AJAX. A video that explains that really well imo is this. Even though the video is about JSONs stored on some webserver it also works for PHP requests