开发者

Linking Post Title to Specific Page ID

I've created a form to update my websites homepage with content but I wanted to know how I could set it up so that a posts title links to a specific post ID. I'd also like to add a Read More link that directs anybody reading the blog to the correct post.开发者_如何学JAVA

Here is my PHP code:

<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>


Wrap your title with an a tag and append the post ID (or some other unique field) to the link, something like:

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

You can then detect if someone is looking for a specific blog post by looking for the id parameter in the querystring:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜