How to reduce amount of data pulled from mysql

Discussion in 'PHP' started by deleted-account, Jun 21, 2009.

  1. #1
    I'm writing a blogging system and I'd like to be able to reduce the amount of info shown before someone is actually viewing the post. Like if they are on the page with all the posts only show say 256 letters at most.

    How can I go about doing this?
     
    deleted-account, Jun 21, 2009 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    You can pull the descriptions from the database and use substr() to only grab a certain amount of characters. So something like:

    
     // .. SQL Stuff
     $description = $row['description'];
    
     // Only get 256 letters
     $description = substr($description, 0, 256);
    
    PHP:
    Hope that helps :) Glad you got your other issue worked out! Feel free to contact me if you have any more problems.
     
    Louis11, Jun 21, 2009 IP
    tarponkeith likes this.
  3. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #3
    You can reduce the number of results returned from the database using LIMIT...

    this will return the first 10 results
    select title, content from tablename limit 0, 10

    this will return results 11 through 20
    select title, content from tablename limit 10, 10

    or if you want shorter descriptions, you can use substr to truncate the string, like this
    $new_variable = substr($old_variable, 0, 255);

    the above code will put the first 255 characters of $old_variable into $new_variable
     
    tarponkeith, Jun 21, 2009 IP
  4. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Thanks substr works perfect
     
    deleted-account, Jun 21, 2009 IP
  5. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    when doing that, i advice to put some number of words instead of number of chracters. This way you will not have broken words.
    and here is a function that will give you an "n" number of words from a string.

    function get_n_words($string, $wordsreturned)
        {
        $retval = $string;   
        
        $array = explode(" ", $string);
        if (count($array)<=$wordsreturned)
             {
            $retval = $string;
            }
        else
            {
            array_splice($array, $wordsreturned);
            $retval = implode(" ", $array)."";
            }
        return $retval;
        }
    PHP:
    The whole string is returned if the number of words is less than the number of words in that string. :cool:
     
    zeronese, Jun 22, 2009 IP
  6. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    For faster query time use MySQL build-in function. So you don't have to use substr to cut your results.
    mysql_query("SELECT id,title,LEFT(post_content,256) as post FROM table")
    PHP:
    that way it will only pull 256 characters from post_content field and store in post
     
    Vbot, Jun 22, 2009 IP
  7. uselessguy

    uselessguy Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    number of words would be better instead of character since there is possibility to cut off of a word
     
    uselessguy, Jun 22, 2009 IP
  8. livedating

    livedating Active Member

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83
    #8
    You can extract 256+max_word_len characters from DB, then use words boundary resize (this one is for UTF-8 input):

    function resize_by_words($text, $max_len=0, $more_str='...') {
    $max_word_len = 50;
    $max_repeated_str = 5;
    $text = utf8::trim($text);

    $text = preg_replace('/\s\s/ms', ' ', $text);
    $text = preg_replace('/(.)(\1){'.$max_repeated_str.',}/ms', '', $text);
    $text = preg_replace('/(.+)(\1\W*){'.$max_repeated_str.',}/ms', '', $text);

    if ($text=='.') $text = '';

    $s = '';
    for ($i=0, $n=0; $i<utf8::strlen($text); $i++) {
    if (!ctype_space($text[$i])) $n++; else $n = 0;
    if ($n>$max_word_len) {$s .= ' '; $n = 0;}
    $s .= $text[$i];
    }
    $text = $s;

    if ($max_len>0 && utf8::strlen($text)>$max_len) {
    for ($i = $max_len; $i>=0 && !ctype_space($text[$i]); $i--) ;
    for (; $i>=0 && ctype_space($text[$i]); $i--) ;
    $text = utf8::substr($text, 0, $i+1).$more_str;
    }
    return $text;
    }

    купить квартиру в киеве продать квартиру в киеве снять квартиру в киеве
     
    livedating, Jun 23, 2009 IP
  9. ironmanv8

    ironmanv8 Active Member

    Messages:
    211
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Louis's method of doing is the most straight forward. That is to pull the whole database record, and then truncate it using the substring method.
     
    ironmanv8, Jun 23, 2009 IP