php - how to load database entries in a css container

49

I'm new to dynamic websites, i have a 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 >
    <div ></div>
    <h2 >Karamantoso</h2>
  </div>
  <div >
    <div ></div>
    <h2 >Yamyom</h2>
  </div>
  <div >
    <div ></div>
    <h2 >Hayom</h2>
  </div>
  <div >
    <div ></div>
    <h2 >Sha</h2>
  </div>
</div>
240

Answer

Solution:

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 justecho 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 yourfeed 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

People are also looking for solutions to the problem: php - Cannot retrive data from the database with first() Laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.