Search code examples
phppdoblogsposts

Display posts in different columns


Below is a code which gets blogs from database and displays it in single column on screen, I want to display blogs in multiple columns may be three,with each column displaying 10 blogs ordered by their id, so how to do that. If it is too long to answer here maybe mentioning any method that is easy to use can help me.

this is how I call posts 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>";    

Solution

  • Using the code below it is quite easy to change the number of columns any time you want.

    <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.

    Notice that your table row starts outside of the loop. Then this line echo "</tr><tr>"; ends your table row and starts a new one each time the number of desired columns has been reached. In the above code it's 3.

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

    EDIT

    Place something similar to this at the top of your page above <doctype><html><head>

    All together it should look similar to 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 your usual manner and change $conn to match your code if necessary.