Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post

    Question How to shorten text...

    Hello
    I'm getting some content from database and I want to display just some part of text on the front page and "read more" after that. How to do that? I tried to use substr(); but it was swallowing other results too.

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi there,

    Could you post some code?

    I don't understand what you mean by "swallowing other results too".

    substr just does what it says on the tin!

  • #3
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    I was using this: $content = substr($content, 0, 200);
    When there are several results on a page, if fist one is more than 200 chars, it was shorting fist post, but it also wasn't displaying other posts below



    PHP Code:
     if($_GET['page']) // Is page defined?

        
    {

            
    $page $_GET['page']; // Set to the page defined

        
    }else{

            
    $page 1// Set to default page 1

        
    }
    $max $settings['max'];
    $cur = (($page $max) - $max);
    $start = ($page 1)*$limit;

    $query "SELECT * FROM news ORDER BY ID DESC LIMIT $cur, $max";
    $counttotal mysql_query("SELECT * FROM news ") or die("HTTP://WWW.TUTORIALS.GE"); // select all records

    $counttotal mysql_num_rows($counttotal); // count records
    //Execute the query.
    $result mysql_query($query) or die("NO SUCH PAGE!");
    while (
    $row mysql_fetch_array($result)) {
    //extract() takes an associative array and treats the keys as variable names and values as variable values.
    extract($row);

    //nl2br() translates all newlines ('n') as HTML tags.
    $content nl2br($content);
    if(
    $settings['mod_rewrite'] == '1'){
    echo 
    "<table><tr><td><b><a href='http://localhost/$id/$title.html'><br>$title</a></b></td></tr>
    <tr><td><small><i>Written by <b>$author</b> on $date</i></small><br />Category: <a href='category/$category'>$category</a></td></tr>
    <tr><td>$content<br><br></td></tr><table>"
    ;
                        }
                        else
                        {

                        echo 
    "<table><tr><td><b><a href='view.php?action=view&id=$id'><br>$title</a></b></td></tr>
    <tr><td><small><i>Written by <b>$author</b> on $date</i></small><br />Category: <a href='view.php?action=viewCat&category=$category'>$category</a></td></tr>
    <tr><td>$content<br><br></td></tr><table>"
    ;
                        }

    }
    $total_pages ceil($counttotal $max);
    if(
    $page 1){ // is the page number more than 1?

                    
    $prev = ($page 1); // if so, do the following. take 1 away from the current page number

                    
    echo "<a href='?page=" $prev "'>« Previous </a>"// echo a previous page link

                    
    }
                    for(
    $i 1$i <= $total_pages$i++) // for each page number

                    
    {

                        if(
    $page == $i// if this page were about to echo = the current page

                            
    {

                                echo 
    "<b>" $i "</b>"// echo the page number bold

                                    
    } else {

    echo (
    '<a href="?page=' .  $i .'"> ' $i ' </a>');

                            }

                    }
                    if(
    $page $total_pages){ // is there a next page?

                        
    $next = ($page 1); // if so, add 1 to the current

                    
    echo '<a href="?page='.$next.'">Next »</a>'// echo the next page link

                        


  • #4
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ok, I see what you are trying to do; I'm looking at the while loop ... what do you mean by "not displaying" exactly? Do you mean that only one table is being displayed?
    Last edited by mlse; 01-23-2008 at 03:34 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    I can't see a substr anywhere in your code example, so I'm not sure quite what you mean, but...

    Try something like:
    PHP Code:
    echo substr($content,0,200); //Echo the first 200 characters
    if (strlen($content) > 200) echo "...\n<a href="?read=$id">Read more!</a>"//Print an ellipsis and a "Read more" link if the content exceeds the amount posted. 

  • #6
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Here's what I built for this type of a situation. I use PHP to see if the content is more than 150 characters. If so, it takes the first 150 characters, looks for a space from the end of the content, and only takes up to that character (so it doesn't chop up a word). Then, I throw on a "..." to the end...you could also throw on a link to "read more" if you want. If the content is <= 150, it just shows the full description.

    PHP Code:
    $view_description '';

    if (
    strlen($description) > 150) {
        
    $view_description substr($description0150);
        
    $temp strrpos($view_description' ');
        
    $view_description substr($description0$temp) . '...';
    } else {
        
    $view_description $description;


  • Users who have thanked JohnDubya for this post:

    M.W.S. (01-23-2008)

  • #7
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks everyone, but especially JohnDubya
    I did it

  • #8
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by M.W.S. View Post
    Thanks everyone, but especially JohnDubya
    I did it
    Way to go!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •