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

Thread: Limiting text

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Limiting text

    Hi Guys,

    I have a field in my database named about and I want to limit the amount of characters outputted (to 100)of this, so I thought it would be done like so:

    PHP Code:
    <?php 

    $query 
    mysql_query("SELECT * FROM players ORDER BY RAND() LIMIT 1") or die(mysql_error()); 
    $text substr($about,0,100); 


    while(
    $row mysql_fetch_array($query)) { 

    echo 
    '<h3>'$row['player_name'] .'</h3>'."\n";
    echo 
    '<div class="profile_image"><img src="images_two/odhiambo_delete.jpg" /></div>';
    echo 
    '<div class="profile_text">'$row['text'] .'</div>'."\n";

    }
    ?>
    However, that does not seem to work, where am I going wrong?

    Thanks

    Chris

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try

    PHP Code:
    <?php 

    $query 
    mysql_query("SELECT * FROM players ORDER BY RAND() LIMIT 1") or die(mysql_error());
    while(
    $row mysql_fetch_array($query)) { 
    $text substr($row['about'],0,100); 
    echo 
    '<h3>'$row['player_name'] ."</h3>\n";
    echo 
    '<div class="profile_image"><img src="images_two/odhiambo_delete.jpg" /></div>';
    echo 
    '<div class="profile_text">'$text ."</div>\n";

    }
    ?>
    HTH
    Dan
    Last edited by whizard; 08-15-2007 at 07:52 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Here's a little function I use to truncate text to the last word within a defined char limit. It takes the text to be truncated and the max char limit as arguements respectively. The returned value is a truncated string under the max char limit and ending in a complete word.

    PHP Code:
    // truncateStringToWord
    // truncates $str [string] to a max char limit
    // and will truncate whole words only.
    // returns the truncated string
    function truncateStringToWord$str,$str_limit )
    {
        
    $str_short current(explode("\n"wordwrap($str$str_limit"\n")));
        return 
    $str_short;

    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

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