views:

38

answers:

4

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

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

webbiedave
How exactly would i implement this into my blog? Does the figure "50" limit the post to display fifty characters? Does the 0 do any important jobs? (Sorry I'm so inquisitive... I'm just trying to learn! XD)
ThatMacLad
Yes. 0 means "start at the beginning of the string." 50 means "return 50 characters at most."
webbiedave
Thanks man! I got it working... you're simply awesome!
ThatMacLad
You're welcome. Glad I could help.
webbiedave
+1  A: 
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).

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

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

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.

dnagirl