SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String restriction

    Hi, i have picked out a string of data from a mysql table.

    lets just say the string is 2000 characters long.
    How can i shorten it and only show or load the first 200 characters

    at the moment i have this:

    PHP Code:
    $article = @mysql_query('SELECT id, subject, maintext FROM ariticle order desc limit 1'); 

    while (
    $text mysql_fetch_array($article)) { 
       
    $id $text['id']; 
       
    $subject $text['subject']; 
       
    $maintext $text['maintext'];
       echo 
    '<p>' $subject 
           
    '</p><p>' $maintext .
           
    '</p>'
     } 
    I want to limit $maintext to 200 characters.

    How can i do this?

    Thanks in advanced.

  2. #2
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    PHP Code:
    $maintext=substr($text['maintext'],0,200); 
    Why do we exist? What is our purpose?
    >H2O Developments
    >USS Endeavour
    >Hosted@Servage

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Look also at the LEFT() function for MySQL. e.g. LEFT(maintext) AS summary

    Unless you need all the characters elsewhere in the page then why not simply get the first 200 characters from the database field anyway in the actual SQL command?

    No point in using more database bandwidth than is necessary - those extra 1800 characters = 1800 bytes of bandwidth per visitor. If you have 1000 visitors then that's an unnecessary extra 1,800,000 bytes of bandwidth used.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah forgot about that one

    Code:
    $article = @mysql_query('SELECT id, subject, LEFT(maintext,200) AS maintext FROM ariticle order desc limit 1');
    Should do the trick
    Why do we exist? What is our purpose?
    >H2O Developments
    >USS Endeavour
    >Hosted@Servage

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's actually ORDER BY fieldname, but yeah.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could possible do a:
    PHP Code:
    $text preg_replace('/\s+\w*$/'''$text); 
    To avoid ending on an incomplete word.

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks you guys

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok this works fine:

    PHP Code:
    $article = @mysql_query('SELECT id, subject, LEFT(maintext,200) AS maintext FROM ariticle order by date desc limit 1'); 
    But how would i pick out the next 200?

    i.e. miss out the first 200 then pick the next?

    David

  10. #10
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Me01273
    ok this works fine:

    PHP Code:
    $article = @mysql_query('SELECT id, subject, LEFT(maintext,200) AS maintext FROM ariticle order by date desc limit 1'); 
    But how would i pick out the next 200?

    i.e. miss out the first 200 then pick the next?

    David
    PHP Code:
    $article = @mysql_query('SELECT id, subject, MID(maintext, 200, 200) AS maintext FROM ariticle order by date desc limit 1'); 

  11. #11
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you plan getting different offsets quite regularly throughout the same script, it may be more effecient to retrieve the whole lot and then use substr to get the required portions of text.

  12. #12
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    No point in using more database bandwidth than is necessary - those extra 1800 characters = 1800 bytes of bandwidth per visitor. If you have 1000 visitors then that's an unnecessary extra 1,800,000 bytes of bandwidth used.
    First of all, it's traffic and not bandwidth. Bandwidth is SPEED.
    And downloading too much from the MySQL server is not such a huge concern as long as you don't have a very active site since you don't pay for this traffic.

  13. #13
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ^^^ Fair comment - it is traffic. But ... it depends on how your hosting is configured. You are right that most hosts won't include database traffic in your bandwidth quota, but if it becomes extreme then they might get tetchy about it. (Most hosts describe data traffic as bandwidth, as do most ISPs).

    However - there's still no point in trafficking more data than is necessary.

    @trib4lmaniac - I agree.
    Ian Anderson
    www.siteguru.co.uk

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with you there^
    However i have my own servers runnning, so i dont have a limit! isnt that good. By the way i can beat any price on webhosting and i garantee to offer a better service.
    Any takers?

    David


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
  •