开发者

How can I Display an extract from MySQL database entry?

I'm after creating a webpage that includes a blog section and it currently displays the whole post on the homepage. I'd like to set it so that it only displays a certain part of the entry i.e 50 words. I'd then like to be able to set it so that I have a read more button below the post that links to the post id. I currently use post.php?=# (# = whatever the post id is).

Here is the homepage:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Blog Name</title> 
<link rel="stylesheet" href="css/style.css" type="text/css" /> 
<body>
  开发者_开发问答  <div id="upper-bar">
    <div id="bar-content">

    <a href="#">Home</a>
    <a href="#">Archives</a>
    <a href="#">Contact</a>
    <a href="#">About</a>

    <a href="#"><img src="images/twitter.png" id="tweet"></a><a href="#"><img src="images/feed.png" id="feed"></a>
    </div>
    </div>

    <div id="clear">
    </div>


    <div class="main">
    <h1>Blog Name</h1>
    <div class="post-col">
    <?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']);
        $id = $row['id'];

        ?>
         <div id='post-info'><?php echo "<p id='title'><strong><a href=\"post.php?id=". $id . "\">" . $title . "</a></strong></p>"; ?><br /></div>
               <div id="post">
               <?php echo $entry; ?>
               <!--<br /><br />
               Posted on <?php echo $date; ?> !-->
               </p>
               </div>


        </p>
    </div>    

        <?php
    }
    ?>
    </div>
     </div> 
</body> 
</html> 


Must it be words? Very long or short words could result in vastly differing character counts. Perhaps you should do a character count instead.

If so, you could use substr to print part of a string (change the 50 to whichever number of characters you want):

<?php echo htmlspecialchars(substr($entry, 0, 50)) ?>... 
<a href="/post.php?id=<?php echo $id ?>">read more</a>

Alternatively, you could select a substring through SQL with SELECT SUBSTRING


SELECT SUBSTRING_INDEX(entry, ' ', 51) as entry FROM php_blog;

This will take up to 50 words for each entry (assuming that words are delimited with a single whitespace).


$truncatedEntry = substr($entry, 0, 50) . '...';

// truncate with word-wrapping
$truncatedEntry = substr($entry, 0, strpos(wordwrap($entry, 50), "\n")) . ' ...';


On your main page, query your blog posts like this:

$sql = "SELECT id, title, blog_timestamp, LEFT(blog_contents, 50) AS txt 
        FROM php_blog ORDER BY timestamp DESC LIMIT 5"; 

Then, when processing the results, trim to the nearest word and add a link.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜