Show posts in different columns

Below is the code that gets blogs from the database and displays it in a single column on the screen. I want to display blogs in several columns, maybe three, and each column displays 10 blogs sorted by their identifier, so how to do it. If it's too long to answer here, maybe mentioning any method that is easy to use can help me.

this is how i call messages from db:

$query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id ORDER BY blogs_id desc LIMIT 10"); foreach($db->query($query)as $row){ $blogs_id = $row['blogs_id']; $title = $row['title']; $body = $row['body']; $posted_by = $row['posted_by']; } echo "<h2>$title</h2> <p>$body</p>"; 
+5
source share
1 answer

Using the code below, it's pretty easy to change the number of columns at any time.

 <tr> <?php do { //horizontal looper?> <td><div><?php echo $row['title']; ?></div> <div><?php echo $row['body']; ?></div> <div style="height:20px;"></div></td> <?php $row = $query->fetch(PDO::FETCH_ASSOC); if (!isset($nested_List)) { $nested_List= 1; } if (isset($row) && is_array($row) && $nested_List++%3==0) { echo "</tr><tr>"; } } while ($row); //end horizontal looper ?> 

This will give you three columns.

Note that the table row starts outside the loop. Then this line is echo "</tr><tr>"; completes your table row and starts a new one each time the number of desired columns is reached. In the above code, this is 3.

To change the number of columns, just change $nested_List++%3==0 to $nested_List++%5==0 and you will have 5 columns.

Happy coding!

EDIT

Put something like this on top of your page above <doctype><html><head>

Together, it should look something like this ...

 <?php $host = 'localhost'; $db = 'database_name'; $user = 'database_user'; $pw = 'database_password'; $conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id ORDER BY blogs_id desc LIMIT 10"; $query = $conn->prepare($sql); $query->execute(); $row = $query->fetch(PDO::FETCH_ASSOC); $totalRows = $query->rowCount(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <table cellpadding="5" cellspacing="0" border="0"> <tr> <?php do { //horizontal looper?> <td><div><?php echo $row['title']; ?></div> <div><?php echo $row['body']; ?></div> <div style="height:20px;"></div></td> <?php $row = $query->fetch(PDO::FETCH_ASSOC); if (!isset($nested_List)) { $nested_List= 1; } if (isset($row) && is_array($row) && $nested_List++%3==0) { echo "</tr><tr>"; } } while ($row); //end horizontal looper ?> </table> </body> </html> 

Of course, you can "include" or "require" your connection file in the usual way and change $conn to match your code, if necessary.

0
source

Source: https://habr.com/ru/post/1232604/


All Articles