Current location: Hot Scripts Forums » Programming Languages » PHP » Display intro text to article with a "more info" button?


Display intro text to article with a "more info" button?

Reply
  #1 (permalink)  
Old 07-05-06, 08:01 PM
Rollo Tamasi Rollo Tamasi is offline
Newbie Coder
 
Join Date: Jun 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Display intro text to article with a "more info" button?

I'm currently pulling out articles from a database but the articles are quite large and when i display more than 3 articles on a single page the page becomes too chaotic with text.

How can only show the first 50 words of the article?

<?php echo $entry; ?> is all i'm using to display the articles and i'm using a straight forward sql statement as well if it makes any difference? :
$sql = "SELECT * FROM php_blog WHERE category='$category' ORDER BY timestamp DESC LIMIT 5";

Thanks
Reply With Quote
  #2 (permalink)  
Old 07-05-06, 08:42 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
This will limit the string to a certain amount of words. I don't know how you plan on displaying individual articles so I'm taking my best guess.
PHP Code:

<?php


function WordLimit$string$id 0$words 50 )
{
    
$stringTmp preg_replace'/\s\s+/'' 'ereg_replace'<[^>]*>'' 'trim$string ) ) );
    
$stringTmp explode' '$stringTmp );
    
$words intval$words );
    if ( 
$words AND count$stringTmp ) > $words )
    {
        
$string '';
        for ( 
$i 0$i $words$i++ )
            
$string .= empty( $string ) ? $stringTmp[$i] : ' ' $stringTmp[$i];
        
$string .= '...';
        
$string preg_replace'/(\.|\,|\;|\:)+(\.\.\.)$/''...'$string );
        if ( 
intval$id ) > )
            
$string .= " (<a href=\"articles.php?id={$id}\">more</a>)";
    }
    return 
$string;
}

// Example
echo WordLimit$row['text'], $row['id'], 50 );

?>
...
Added a conditional check to make sure $words > 0... you know, just in case.
__________________
The toxic ZCE

Last edited by Keith; 07-05-06 at 09:07 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
You can drive insane traffic to your web site using the magic of Article Submitter hostthebest General Advertisements 0 05-13-06 10:33 PM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 03:27 PM
how to display info from a text box in a text area? Ademuth PHP 7 08-31-05 12:58 PM
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM


All times are GMT -5. The time now is 07:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.