SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Breaking an article into several pages

    Hello folks.

    I am working on a site that has several long articles on it. I want to be able to break these articles up into pages, based on how long they are.

    The only way I can think to do this, is to read the article in from the database, and then check the string length, and if it over a certain number of characters, display the first say, 800 and then a link to page 2.

    Is this the best way to achieve this functionality, or is there something better?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hm, it depends on how are your articles structured, do they contain html? Obviously you don't want to break them in the middle of sentence?
    Saul

  3. #3
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think the best way to achieve this would be take the string from the database and divide the string up into an array and then pass a variable from page to page to select which part of the array you want to display.

    You might run into some problems with cutting off a word in the middle of it by using a straight character count. So, I would suggest developing some AI that say if it reaches the end of your character count, it looks for the </p> and that is end of your chunk for the array, then start back up at the next <p> in the article.

  4. #4
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The articles contain basic HTML (paragraphs, strong, emphasis, links, etc).

    Can anyone provide me some example code of how to break the pages at paragraph breaks?

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Could go like this:
    Code php:
    $data=explode('</p>',$data);
    $c=count($data);
     
    //display first three paragraphs
    for($i=0; $i<$c && $i<=3; $i++){
      echo $data[$i].'</p>';
    }
    Saul

  6. #6
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. I'll play with that and see what I can work out.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Virginia
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or to be more exact, you can use the above to manually start a new page. Put in an html commet and search for the comment instead of the p tag to break the page.

    You can do a countless number of things, but make sure that your breaking it into pages for the right reasons -> one coherent subject or topic at a time and not just splitting a nice article into 4 or 5 pages.
    Mark A. Drake
    - Mark A. Drake
    - OnSlaught

  8. #8
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestions guys.

    Now what I am trying to do, is create a script that will let this span over more than 1 page.

    I created a variable called $start that holds the paragraph number that should be the number of the first paragraph displayed. So if you went to page two, the URL might look like this

    Code PHP:
    article.php?id=145&start=4

    And then the script to display my text is this
    Code PHP:
    $start = (int) $_GET['start'];
    if($start>0){
    				echo "
    					<p>".$articleBody[$start]."</p>
    					<p>".$articleBody[$start+1]."</p>
    					<p>".$articleBody[$start+2]."</p>
    				";

    My next questions:

    is there a better way to do this (perhaps using a page number instead of a start number)?

    And how would I create a loop that will check the total number of paragraphs, and create links to the pages accordingly? For example, if I have it to display 3 paragraphs per page, and there are 9 paragraphs total, it'll generate 3 links

    [Page 1][Page 2][Page 3]

    (an example would be greatly appreciated)

    Thanks everyone!

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Something like this:

    Code php:
    $perpage=3;
    $start=(int)$_GET['start']*$perpage;
     
    $data=explode('</p>',$data);
    $c=count($data);
     
    for($i=$start; $i<$c && $i<=$start+$perpage; $i++){
      echo $data[$i].'</p>';
    }
     
    $pages=ceil($c/$perpage);
     
    for($i=0; $i<$pages; $i++){
      echo "<a href=\"article.php?id=$article_id&start=$start\">[ Page ".($i+1)." ]</a> "; 
    }
    Saul


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
  •