用数据库制作动态页面?

时间:2018-05-28 15:21:59

标签: php mysql database

这就是我要做的事情。我试图回应存储在我的数据库中的博客帖子。很简单,但我想让他们被重定向到view_post.php以显示完整的帖子,当他们点击小预览时。这是我的代码:

<?php
session_start();
require_once('required/db.php');
$_SESSION['admin'] = false;
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>WillWam - Blog</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="assets/css/style.css" type="text/css">
  <link rel="ico" href="assets/favicon.ico">
  </head>
  <body>
    <nav><h2 class="title">The Blog</h2></nav>
    <?php

    $sql="SELECT id,title,author,body FROM posts ORDER BY id DESC";

    if ($result=mysqli_query($con,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
      printf('<a href="view_post.php"><div class="row"><div class="row-inner"><p><strong>%s</strong> | %s |</p></div></div></a>', $row[0],$row[1],$row[2]);
    }
  // Free result set
  mysqli_free_result($result);
}


mysqli_close($con);

?>
    <div class="top-margin wrapper"><div class="container"><p>Administrator? <a href="/admin/">Click here</a>.</p></div></div>
  </body>
</html>

我如何动态地将预览行设置为链接(例如view_post.php?id = 1)?我会在view_post.php中添加什么内容?

1 个答案:

答案 0 :(得分:0)

$row包含id时,您可以创建这样的链接

printf('<a href="view_post.php?id='.$row['id'].'"> <----just put the id there
                                                       And on view_port use the value
<div class="row"><div class="row-inner">
<p><strong>%s</strong> | %s |</p></div></div></a>', $row[0],$row[1],$row[2]);