Using the code below, it's pretty easy to change the number of columns at any time.
<tr> <?php do { <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);
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 { <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); </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.
source share