SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    US
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    splitting text into pages in php

    Hi All,

    Does anyone know or have a script that splits the long text called from database into multiple pages ..... just like its done in the articles here on sitepoint.com .... I need the solution to this badly .... I was not able to find it over net. Anyone there to help.

  2. #2
    SitePoint Addict Andrei P.'s Avatar
    Join Date
    Jul 2004
    Location
    New York, New York
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One solution would be to insert some tags in the form where you insert the text:

    HTML Code:
    <Page1>
    Content of page one goes here...
    </Page1>
    
    <Page2>
    Page two goes here
    </Page2>
    And then to parse it with PHP and in the first page, with the help of the query string display page one:

    http://somesite.com/article.php?page=1

    Query the database for the article and then with a while loop output only what's between:

    PHP Code:
    echo "<Page".$HTTP_GET_VARS['page'].">"
    and

    PHP Code:
    echo "</Page".$HTTP_GET_VARS['page'].">"

  3. #3
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think bionicsamir is asking how do you chunk the longer text into separate pages (as compared to how to access the pages once they are smaller). Like do you search for a set number of characters, or line breaks, or what... how would you parse the data to make the separate pages?

  4. #4
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should try to get your text into an array.

    If your text comes from an external file:
    PHP Code:
    $fileContents file './path_to_file/file.txt' ); 
    Otherwise, if you have it as a string, try:
    PHP Code:
    $fileContents explode "\n"$yourText );    // Splits the text at NEWLINES 
    You now have an array. Suppose you want to show the first 25 pages
    PHP Code:
    $i 0;
    while ( 
    $i 25 
    {
      echo 
    $line "<BR>\n";
      
    $i++;

    Lines 25 -> 50
    PHP Code:
    $i 25;
    while ( 
    $i 50 )
    {
      echo 
    $line "<BR>\n";
      
    $i++;

    Etc etc etc

    I suppose you could work out some smart formula that would allow this:
    PHP Code:
    // Link: yourpage.php?page=2
    $linesPerPage 25;
    $startLine $linesPerPage $_GET['page'];
    $endLine $startLine $linesPerPage;

    while ( 
    $startLine $endLine )
    {
      echo 
    $fileContent$startLine ];
      
    $startLine++;


    Quaint Tech
    - Blog on web development and web technology.

  5. #5
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the method used for this is called paging,

    theres a nice thread over here

    good luck

  6. #6
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    US
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hold on.... if I am not mistaken then you are talking about pagination ... which is taking the rows from database and displaying in seperate pages... e.g. 10 rows in first page and next 10 on second page etc...

    but what I want to do is this

    In my database there is a field with the name body which has article in it .... the article is about 2000 words.
    I want the text in article (the text is in html format) to be chopped and displayed in seperate pages ... for e.g. 500 words per page ... therefore the article will come in 4 pages.
    And obviously I dont want the pages to be seperated in the middle of the word ... for e.g. there is a word
    'Development' and it should not be such that Deve is on one page and lopment goes to other page ...

    The above is what I want to achieve .... any help...

  8. #8
    SitePoint Addict Andrei P.'s Avatar
    Join Date
    Jul 2004
    Location
    New York, New York
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have the exact code, but as I said this can be accomplished by inserting tags in your content:


    <Page1>
    Content of page one goes here...
    </Page1>

    <Page2>
    Page two goes here
    </Page2>

    and then with PHP parsing them, most probably with the stristr() function, so that you display only what's inside the <Page1></Page1> tags, for the first page.

    For example I created a code (uses stristr()) that parses the text from the database before outputting it, and highlights the content between <code></code> tags.

  9. #9
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is really a pain up your butt, cause you need to add those tags.

    Quote Originally Posted by Andrei P.
    I don't have the exact code, but as I said this can be accomplished by inserting tags in your content:


    <Page1>
    Content of page one goes here...
    </Page1>

    <Page2>
    Page two goes here
    </Page2>

    and then with PHP parsing them, most probably with the stristr() function, so that you display only what's inside the <Page1></Page1> tags, for the first page.

    For example I created a code (uses stristr()) that parses the text from the database before outputting it, and highlights the content between <code></code> tags.
    Instead use my code, but add this:

    PHP Code:
    $yourText wordwrap $yourText 60"\n" );
    // This will split the lines at 60 characters (see wordwrap entry in PHP doc!)
    // Above is intelligent -> wraps AROUND words, leaving them whole

    // Then use old method:
    $fileContents explode "\n"$yourText );

    // Link: yourpage.php?page=2
    $linesPerPage 25;
    $startLine $linesPerPage $_GET['page'];
    $endLine $startLine $linesPerPage;

    while ( 
    $startLine $endLine )
    {
      echo 
    $fileContent$startLine ];
      
    $startLine++;

    See my previous post for explanation, but this should work fine!!!

    Quaint Tech
    - Blog on web development and web technology.


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
  •