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 4 of 4
  1. #1
    Regular Coder maxvee8's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    185
    Thanks
    19
    Thanked 18 Times in 18 Posts

    Question Only Display a certain amount of words with a View more button after how?

    hi,

    I have:

    Table : sweets

    Fields: sweetname, Description

    i want to select the description and the title print them out but only print out say print 15 words from the description and then print a view more button

    how is this done? please give a simple example if possible as im not sure

    example below



    max
    Last edited by maxvee8; 05-09-2008 at 08:16 PM.
    "I have not failed, I've found 10,000 ways that don't work" Thomas Edison

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    You could use
    PHP Code:
    <?php

    $string 
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
    $string explode(" "$string);

    $i 0;
    while (
    $i 15)
    {
      echo 
    $string[$i]." ";
      
    $i++;
    }
    // Outputs Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has 

    ?>
    or... an alternative method not on words..
    PHP Code:
    <?php

    $string 
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
    echo 
    substr($string0100)."...";
    // Outputs Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the ...

    ?>
    I would prefer the second method cause the first method relies on word length and you can get some long words..
    Last edited by Iszak; 05-09-2008 at 11:31 PM. Reason: added outputs

  • Users who have thanked Iszak for this post:

    maxvee8 (05-11-2008)

  • #3
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Fwiw, I put together this page a while back to compare two functions for excerpting to specified lengths.

    The trimTextChars() function which I'd written allows me to excerpt text to a specific max character length, but without producing partial words at the end.

    http://dev.akabillposters.com/php/truncated-text/

    PHP Code:
    // trim to N characters
    function trimTextChars($src_text$char_count) {

        
    $char_count = (isset($char_count)) ? $char_count 100# if $char_count is not set, it defaults to 100
        
    $subtxt substr($src_text,0,$char_count);
        
    $xslength strlen(strrchr($subtxt,' '));
        return 
    substr($subtxt,0,$char_count-$xslength).'&hellip;';


    Last edited by Bill Posters; 05-09-2008 at 11:58 PM.

  • #4
    Regular Coder maxvee8's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    185
    Thanks
    19
    Thanked 18 Times in 18 Posts
    i used iszaks 2nd example just for simplicity worked perfect .. thanks max
    "I have not failed, I've found 10,000 ways that don't work" Thomas Edison


  •  

    Posting Permissions

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