如何将SQL结果显示到网格列中

时间:2019-02-28 07:49:08

标签: php html

我想从数据库中获取帖子,我能够从数据库中获取所有行,但是我如何将它们显示在模板视图中,以便它可以将所有行显示到该模板中。

这是我的php查询

<?php
    include('connect.php');
    $q = "select title, image, summary, desc from posts";
    $rs = mysqli_query($con,$q);
    $data = mysqli_fetch_all($rs, MYSQLI_ASSOC);
    //print_r($data);
?>

而且我想将行数据回显到此模板视图中,我该怎么做?

<!-- Grid column -->
<div class="col-lg-4 col-md-4 col-sm-4">

    <!-- Featured image -->
    <div class="view overlay rounded z-depth-2 mb-4">
        <a href="post/<?php echo $title; ?>"><img class="img-fluid" src="upload/<?php echo $image?>" alt=""></a>
    </div>
    <a href=""><h4 class="font-weight-bold mb-3"><strong><?php echo $list->title; ?></strong></h4></a>
    <p class="dark-grey-text"><?php echo $summary; ?></p>

</div>
<!-- Grid column -->

1 个答案:

答案 0 :(得分:3)

如果您不使用任何框架,则应在同一文件中编写HTML并遍历数据:

<?php foreach ($data as $value) { ?>
<!-- Grid column -->
<div class="col-lg-4 col-md-4 col-sm-4">

  <!-- Featured image -->
  <div class="view overlay rounded z-depth-2 mb-4">
    <a href="post/<?php echo $value['title']; ?>"><img class="img-fluid" src="upload/<?php echo $value['image']?>" alt=""></a>
  </div>
  <a href=""><h4 class="font-weight-bold mb-3"><strong><?php echo $list->title; ?></strong></h4></a>
  <p class="dark-grey-text"><?php echo $value['summary']; ?></p>

</div>
<!-- Grid column -->
<?php } ?>