Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display each row from MySQL in a separate DIV

Tags:

loops

php

mysql

I have a posts table which contains a field for the posts. Let's say I have 10 posts that i want to show in 10 divs. How should I proceed in doing that? I've managed to get the full contents using a while loop, but that only shows the full contents in one place, and I want to have individual divs (so i can use different background colors) for each individual post.

Help me out please, I hope it makes sense. Just think at the way facebook displays posts for example. Each post has it's own box. I want something similar.

Snippet of the code I have to get the posts is available here:

<?php
require_once("includes/database.php"); // Get the database connection

$get_post = "SELECT full_post FROM posts";
$show_post = mysqli_query($connection, $get_post);

if (!$show_post) {
echo "Could not load post. " . "(" . mysqli_error($connection) . ")";
}
while ($post = mysqli_fetch_assoc($show_post)) {
echo $post["full_post"] . "<br />";
}
mysqli_free_result($show_post);

?>
like image 356
Popa Andrei Avatar asked Jan 26 '26 03:01

Popa Andrei


1 Answers

The easiest method of doing this is creating the divs from the while function that's showing your posts and adding CSS classes to them.

Example:

while ($post = mysqli_fetch_assoc($show_post)) {
echo '<div class="blue">';
echo $post["full_post"] . "<br />";
echo '</div>';
}
like image 110
Alex Avatar answered Jan 27 '26 17:01

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!