SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Was wondering if someone could help me, I want to select the first paragraph from an article and display it on a page as a summary. Right now I am doing it with php's explode() and I thought there might be a more economical way of doing it, so that it didn't have to sift through the whole article searching for the <p>'s. Any suggestions would be appreciated.

  2. #2
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming the whole text is in a string...

    I would think that you could use strpos() to find the position of the first </p> (assuming all paragraphs are closed, as they should be). That will get you the position at which the first paragraph ends, and then just use that in a substr() or whatever to trim it out of the string.
    eh?

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, here's something I do...

    PHP Code:
        $words 50;
        
    $num $words 15;
        
    $result mysql_query("SELECT title, LEFT(body, $num) FROM news");
            while (
    $row mysql_fetch_array($result)) {
              
    $body $row["body"];
            }
            
    $array explode(" "$body);

            for (
    $i 0$i <= count($array); $i++) {
              echo(
    "$array[$i] ");
            } 
    Simply changing the $words variable (and for all of you SNL fans: SWORDS!) to whatever number of words you would like to appear.

    If you wanted to display something like "..." at the end of the last word of the 50 with a link to the rest, you could use a simple conditional inside of the FOR loop.

    It's not perfect, but it works pretty well for me, and in the above case, it will only select 750 characters out of the entire article to makeup the summary.

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had thought about doing like that, but because I wanted the flexibility of being able to have different amounts of words for each summary decided to rather just split it up into the paragraphs.

    As for singularity's comments; I don't think I can use substr() to trim the summary out of the string, but it is a good idea to use strpos() to find the first position of the </p> or <p>. Any ideas on what function I could use to crop the information out of the string up to what was defined in strpos()?

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    SELECT SUBSTRING_INDEX(body,"<p>",2) ...

    This returns all the text to the left of the second <p> in the 'body' column. If you don't start your first paragraph with a <p>, then change the 2 to a 1 to return everything to the left of the first <p>.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having a bit of trouble with the SELECT SUBSTRING_INDEX command.

    This is what I have got:
    Code:
    $summary = mysql_query ("SELECT SUBSTRING_INDEX('content','<p>',2)");
    And what I get is: Resource id #3 with a different number in each summary where the summary should be.

    Also where content is in the above code, should that be the column of article text, or should I put in the article string ie $content?

  7. #7
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $summary = mysql_query ("SELECT SUBSTRING_INDEX('content','<p>',2)");

    where i the rest of your query ? ... "FROM table WHERE var='asdf'" ?
    cogito, ergo sum

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake, I apologise, I somehow forgot to put that stuff in it!

    Here is the improved version:

    PHP Code:
    $summary =    mysql_query ("SELECT SUBSTRING_INDEX('$content','<p>',2) FROM news WHERE id=$id"
    Even with this stuff in it it still just displays like Resource id #2, or #3 etc where each of the summary's should go. I made a little script on its own with just:
    PHP Code:
    $summary =    mysql_query ("SELECT SUBSTRING_INDEX('helloghelloghellog','g',2)"
    and instead of getting helloghellog it just displayed Resource id #2. I would love to know where it is getting these resource id's from!

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to assign it an alias
    PHP Code:
    $summary =    mysql_query ("SELECT SUBSTRING_INDEX('content','<p>',2) as para1 FROM news WHERE id=$id");
    $row mysql_fetch_array($summary);
    print 
    $row["para1"]; 
    Last edited by freddydoesphp; Feb 23, 2001 at 22:47.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks heaps, I knew there must have been something missing from it!


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
  •