如何在循环中使用bootstrap?

时间:2015-06-30 11:50:38

标签: php twitter-bootstrap

我在php上创建了一个博客,我想使用bootstrap来发布我的帖子Bootstrap Jumbotron example

我生成帖子的代码是:

try {
    $stmt = $db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
    while($row = $stmt->fetch()){
        echo '<div>';
        echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>';
        echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
        echo '<p>'.$row['postDesc'].'</p>';
        echo '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';
        echo '</div>';
    }
} catch(PDOException $e) {
    echo $e->getMessage();
}

如何将此循环格式化为示例?

2 个答案:

答案 0 :(得分:0)

        echo '<div class="col-md-4">';
            echo '<h2><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h2>';
            echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
            echo '<p>'.$row['postDesc'].'</p>';             
            echo '<p><a role="button" href="#" class="btn btn-default" href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';               
        echo '</div>';

答案 1 :(得分:0)

取决于您每行的数量:您可以使用col-sm-4 col-sm-3或col-md-3或col-md-4

 //to have 3 on the same line
 echo '<div class="col-sm-4">';
     echo '<h2><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h2>';
     echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
     echo '<p>'.$row['postDesc'].'</p>';             
     echo '<p><a role="button" href="#" class="btn btn-default" href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';               
 echo '</div>';


//to have 4 on the same line
echo '<div class="col-sm-3">';
     echo '<h2><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h2>';
     echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
     echo '<p>'.$row['postDesc'].'</p>';             
     echo '<p><a role="button" href="#" class="btn btn-default" href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';               
echo '</div>';