将帖子标题链接到特定页面ID

时间:2010-04-21 16:09:19

标签: php mysql html

我已经创建了一个表单来更新我的网站主页,但我想知道如何设置它以便帖子标题链接到特定的帖子ID。我还想添加一个Read More链接,指导任何阅读博客的人都找到正确的帖子。

这是我的PHP代码:

<html>

<head>
<title>Blog Name</title>
</head>

<body>

<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('tmlblog');

$sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5";

$result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {

    $date = date("l F d Y", $row['timestamp']);

    $title = stripslashes($row['title']);
    $entry = stripslashes($row['entry']);
    $password = $row['password'];
    $id = $row['id'];

    if ($password == 1) {
        echo "<p><strong>" . $title . "</strong></p>";

        printf("<p>This is a password protected entry. If you have a password, log in below.</p>");

        printf("<form method=\"post\" action=\"post.php?id=%s\"><p><strong><label for=\"username\">Username:</label></strong><br /><input type=\"text\" name=\"username\" id=\"username\" /></p><p><strong><label for=\"pass\">Password:</label></strong><br /><input type=\"password\" name=\"pass\" id=\"pass\" /></p><p><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /></p></form>",$id);
        print "<hr />";
    }
    else { ?>

        <p><strong><?php echo $title; ?></strong><br /><br />
        <?php echo $entry; ?><br /><br />
        Posted on <?php echo $date; ?>

        <hr /></p>

        <?php
    }
} 
   ?>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

使用a标记包裹您的标题,并将帖子ID(或其他一些唯一字段)附加到该链接,例如:

echo "<p><strong><a href=\"?id=". $id . "\">" . $title . "</a></strong></p>";

然后,您可以通过在查询字符串中查找id参数来检测某人是否正在查找特定的博客帖子:

if (isset($_GET["id"])) {
    // display blog entry
} else {
    // display your blog's front page
}