php - 简单的博客脚本 - 重定向到具有给定内容的页面

时间:2014-03-22 21:45:07

标签: php html mysql database

我正在建立一个博客,我已经完成了网页,其中显示了所有帖子的标题,图片和简短描述(每个帖子=一张图片,一个标题和一个描述)。接下来我想在点击所选标题后启用将用户重定向到包含完整帖子的页面,但我不知道从哪里开始。

home.php

<?php 
session_start();
require_once('connection.php');

mysql_select_db($database_connection, $connection);
$query_post = "SELECT postID, title, date, category, description, pimage, text FROM post";
$post = mysql_query($query_post, $connection) or die(mysql_error());
$row_post = mysql_fetch_assoc($post);
$totalRows_post = mysql_num_rows($post);

mysql_select_db($database_connection, $connection);
$query_joining = "SELECT * FROM image inner join post on post.pimage = image.imagename";
$joining = mysql_query($query_joining, $connection) or die(mysql_error());
$row_joining = mysql_fetch_assoc($joining);
$totalRows_joining = mysql_num_rows($joining);
?>


<ul>
<?php
while($row_joining = mysql_fetch_assoc($joining)) {     
?>
  <li>
   <a href="post.php"><img src="images/<?php echo $row_joining['imagename']; ?>"></a>
   <h3><a href="post.php"><?php echo $row_joining['title']; ?></a></h3>
   <p><?php echo $row_joining['description']; ?></p>
   <a href="post.php">Read more</a>
  </li>
<?php
}
?>   
</ul>

post.php

<div>
  <h1><?php echo $row_post['title']; ?></h1>
  <img "images/<?php echo $row_post['pimage']; ?>">
  <h4><?php echo $row_post['date']; ?>, <?php echo $row_post['category']; ?></h4>
  <p><?php echo $row_post['text']; ?></p>
</div>

我知道特别是post.php需要更改,但我不知道如何继续,以便所选帖子将显示在post.php

1 个答案:

答案 0 :(得分:2)

当你显示所有帖子时,你可以在href中这样写:

<a href="post.php?post='.$row_post['postID'].'">

然后在post.php中,您可以执行以下操作:

if (isset($_GET['post']))
{
   // check if post is integer
   // check if post exist in database
   // show content from that post only. WHERE postID = data

}
   else
   {
      // list all posts.
   }