SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question limiting field view to X words - problem with loop

    Hi there,

    I have function which takes text from mysql and displays limited number of words with [more]. It also search for nearest space to avoid breaking the word in a middle.
    It was compiled from 2 other func. so I have a little problem with infinite loop in while statement..

    function is:

    function Cut($article_desc,$url) {
    return CutConvert($article_desc).'...' . '[<a href="'.$url.'">more</a>]';
    }

    function CutConvert($str, $index=30, $char=" ") {
    while (substr($str, $index, 1)!=$char) $index++;
    return substr($str, 0, $index);
    }



    How to fix potential infinite loop in 'while'?
    In the code above if the last character in the $str is
    not blank, it will loop forever because the substr() will return an empty string "" and "" <> " ", so it loops forever...


    pls help

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can actually do this in your query:

    SELECT SUBSTRING_INDEX(field, ' ', 30) as shortext from table;

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unfortunately, I cannot

    ..becouse query is generated by CodeCharge software, and code I need have to be included manually as 'global' funcion, then recalled in specific page.

    So I can put parameter (number of chars) in 2 places only:
    - at function definition
    - at recall function (with specific db field) on php page.

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why can't you just edit the code created by CodeCharge, or possibly code it yourself?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    becouse I'm still working and regenerating whole site 20 times a day..
    and I know site will be under continuous developing (using Codecharge) in the future..


Bookmarks

Posting Permissions

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