I know this has been asked all over many times, but I've searched endlessly and have yet to find a good explanation and code of how to do this.

Here's my setup:
PHP/MySQL

Table: "article"
Column: "content"

I have text in the "content" column, which is the actual article. It's long and spans a few pages and I want to split the result into multiple pages. I have it setup like this:

-Text1
[PAGEBREAK]
-Text2
[PAGEBREAK]
-Text3
[PAGEBREAK]
and so on......

I came up with this code:

<?php
if (!isset($page)) $page = 0;
$article = explode($row_articles['content'], "[PAGEBREAK]");
print $article[$page];
?>

But this is where I'm lost. It's not showing anything, so obviously there's something wrong here. Can anyone tell me what's wrong with it? I want to display (from the example above) "Text1", then it stops because it says "[PAGEBREAK]", then shows Next - Prev links, then reloads the page and displays the next part "Text2" and so on.....

That's the other thing that I can't figure out - Next Prev links. I found some code but all it does is reload the page with "PHP_SELF" and shows the same info.

Also, at first I was using split() but then I read that explode() would be better for this. Is that true?

Thanks.

    Nevermind, I figured it out by checking Sitepoint's book (LOL I completely forgot about it and didn't think it would be in there either!).

    Here's the code in case anyone else needs it.

    The first part, to connect and setup.

    <?php
    mysql_select_db($database_main, $main);
    $query_articles = "SELECT * FROM articles WHERE ID = whatever the ID is for that article";
    $articles = mysql_query($query_articles, $main) or die(mysql_error());
    $row_articles = mysql_fetch_assoc($articles);
    $totalRows_articles = mysql_num_rows($articles);
    $articlestext = $row_articles['content'];
    $id = $row_articles['ID'];
    ?>
    

    Then the guts:

    <?php
    //If no page specified, default to the first page ($page = 0)
    if (!isset($_GET['page'])) $page = 0;
    else $page = $_GET['page'];
    
    //Split the text into an array of pages
    $textarray=split('\[PAGEBREAK]',$articlestext);
    
    //Select the page we want
    $articlestext=$textarray[$page];
    
    $PHP_SELF = $_SERVER['PHP_SELF'];
    if ($page != 0) {
    $prevpage = $page - 1;
    echo ("<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">". 'Previous Page</a></p>');
    }
    
    echo ("<p>$articlestext</p>");
    
    if ($page < count($textarray) - 1) {
    $nextpage = $page + 1;
    echo ("<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">". 'Next Page</a></p>');
    }
    
    ?>
    

    Have fun!

      6 years later

      Hi..

      here is the url where i need pagination

      http://www.rintlbd.com/index.php?id=1

      and this is the editor url ( ck editor)

      http://www.rintlbd.com/ck.php

      By the editor i put some paragraphs on it. and end of the paragraph i write {newpage] then started a new paragraph. pagination navigations comes.but when i click the page 2. the same apperas assame as page 1. and the text {newpage} exist on the article

      here is the full code

      <?php mysql_connect('mysql10', 'dlegu', 'dlegu') or die('Connection Failed'); 
      mysql_select_db('legu') or die('Database not found'); 
      $iDefaultRecord = 1;  
      $id = isset($_GET['id']) ? filter_var($_GET['id'], FILTER_VALIDATE_INT) : $iDefaultRecord; $result = mysql_query("SELECT title, dtl, meta_d, meta_k FROM page WHERE id=$id"); if (!mysql_num_rows($result)) { header('Location: 404.php', true, 404); die();}$row = mysql_fetch_array($result);$title = stripslashes($row['title']);$dtl = stripslashes($row['dtl']);$meta_d = stripslashes($row['meta_d']);$meta_k = stripslashes($row['meta_k']); $pages = explode("{newpage}", $dtl); if(isset($_GET['page']) && $_GET['page'] > 0)
      {
      $current_page = $_GET['page'];
      if($current_page > count($pages))
      {
      $current_page = count($pages);
      }
      }
      else
      {
      $current_page = 1;
      } $description = $pages[$current_page - 1]; $navigation = "Pages: ";
      for ($i = 1; $i <= count($pages); $i++)
      {
      if($i == $current_page)
      {
      $navigation .= "<b>";
      }
      $navigation .= "<a href='?page=" . $i . "'> " . $i . "</a> ";
      if($i == $current_page)
      {
      $navigation .= "</b>";
      }
      } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="keywords" content="<?php echo $meta_k; ?>" /> <meta name="description" content="<?php echo $meta_d; ?>" /> <title><?php echo $title; ?></title> <link rel="stylesheet" href="cssReset.css" /> <link rel="stylesheet" href="style.css" /> <style type="text/css">
      img, div, a, li, span{behavior:url(iepngfix.htc);} </style> </head> <body> <div id="top"> <div id="banner"> <h1><a href="#">Environmental.com</a> <span>site slogan here</span></h1> </div> <p id="help">Help Line 24 /7 Support 1800 3246 345</p> <div id="nav"> <ul> <li><a href="index.php?id=1" class="pageitem">index</a></li> <li><a href="index.php?id=2" class="pageitem">apple</a></li> <li><a href="?id=3" class="pageitem">banana</a></li> <li><a href="index.php?id=4" class="pageitem active">orange</a></li> <li><a href="index.php?id=5" class="pageitem">lemon</a></li> <li><a href="index.php?id=6" class="pageitem">nut</a></li> </ul> <div style="clear:both"></div> </div><!--end of navigation--> <div id="glow"> </div> <div style="clear:both"></div> </div><!-- end of top--> <div style="clear:both"></div> <div id="content"> <div id="left"> <div id="box1"> <img src="Images/hand.png" alt="Hand" /> <h3>below content will come from database</h3> <?php echo $dtl; ?> </div><!-- end of box1--> <div style="clear:both"></div> <!-- end of box2--> <div style="clear:both"></div> <!-- end of box3--> <div style="clear:both"></div> <div id="read"> </div> </div><!-- end of left div--> <div id="right"> </div> </div><!-- end of right div--> <div style="clear:both"></div> </div><!-- end of content--> <div style="clear:both"></div> <div id="footer"> </div><!-- end of footer--> </body> </html>

      can anybody help?

        You always

        <?php echo $dtl; ?>
        

        which means you always show the same contents. Use $description, which is assigned the value of $pages[$current_page - 1] instead. I'd also recommend keeping $current_page in the range 0 .. count($pages) - 1, since $current_page - 1 reads like "the previous page" (to me at least). I.e.

        $current_page = $_GET['page'] - 1;
        

          Can you please examine my code again if you dont mind? after your suggestion my code is

          <?php mysql_connect('mysql10', 'dlegu', 'dlegu') or die('Connection Failed');
          mysql_select_db('legu') or die('Database not found');
          $iDefaultRecord = 1;

          $id = isset($GET['id']) ? filter_var($GET['id'], FILTER_VALIDATE_INT) : $iDefaultRecord;
          $result = mysql_query("SELECT title, dtl, meta_d, meta_k FROM page WHERE id=$id");
          if (!mysql_num_rows($result)) { header('Location: 404.php', true, 404); die();}$row = mysql_fetch_array($result);$title = stripslashes($row['title']);$dtl = stripslashes($row['dtl']);$meta_d = stripslashes($row['meta_d']);$meta_k = stripslashes($row['meta_k']);

          $pages = explode("{newpage}", $dtl);

          if(isset($GET['page']) && $GET['page'] > 0)

          {

          $current_page = $_GET['page']-1;
          if($current_page > count($pages))

          {

          $current_page = count($pages);

          }

          }

          else

          {

          $current_page = 1;

          }

          $description = $pages[$current_page - 1];

          $navigation = "Pages: ";

          for ($i = 1; $i <= count($pages); $i++)

          {

          if($i == $current_page)

          {

          $navigation .= "<b>";

          }

          $navigation .= "<a href='?page=" . $i . "'> " . $i . "</a> ";

          if($i == $current_page)

          {

          $navigation .= "</b>";

          }

          }

          ?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          <meta name="keywords" content="<?php echo $meta_k; ?>" />
          <meta name="description" content="<?php echo $meta_d; ?>" />

          <title><?php echo $title; ?></title>
          <link rel="stylesheet" href="cssReset.css" />
          <link rel="stylesheet" href="style.css" />
          <style type="text/css">

          img, div, a, li, span{behavior:url(iepngfix.htc);}
          </style>

          </head>

          <body>

          <div id="top">

              <div id="banner"> 
          
          <h1><a href="#">Environmental.com</a> <span>site slogan here</span></h1> 

          </div>

          <p id="help">Help Line 24 /7 Support  1800 3246 345</p> 
          
          
          
          <div id="nav"> 
          
              <ul> 
          
                      <li><a href="index.php?id=1" class="pageitem">index</a></li> 
                  <li><a href="index.php?id=2" class="pageitem">apple</a></li> 
                  <li><a href="?id=3" class="pageitem">banana</a></li> 
                  <li><a href="index.php?id=4" class="pageitem active">orange</a></li> 
                  <li><a href="index.php?id=5" class="pageitem">lemon</a></li> 
                  <li><a href="index.php?id=6" class="pageitem">nut</a></li> 
          
              </ul> 
          <div style="clear:both"></div> 
          </div><!--end of navigation--> 
          
          <div id="glow"> 
          
          
          </div> 

          <div style="clear:both"></div>
          </div><!-- end of top-->

          <div style="clear:both"></div>

          <div id="content">

                      <div id="left"> 
          
          
                      <div id="box1"> 
          
                              <img src="Images/hand.png" alt="Hand" /> 
                          <h3>below content will come from database</h3> 
                          [B]<?php echo $description; ?> [/B]                 
                      </div><!-- end of box1--> 
          
                                      <div style="clear:both"></div> 
          
          
                     <!-- end of box2--> 
                                      <div style="clear:both"></div> 
          
                      <!-- end of box3--> 
          
                                      <div style="clear:both"></div> 
                      <div id="read"> 
          
          
          
                      </div> 
          
          
          
               </div><!-- end of left div--> 
          
          
               <div id="right"> 
          
          
          
          
          
          
          
          
                  </div> 
          
          
               </div><!-- end of right div--> 

          <div style="clear:both"></div>

          </div><!-- end of content-->

          <div style="clear:both"></div>

          <div id="footer">

          </div><!-- end of footer-->

          </body>
          </html>

          i changed the bold part as you say

            I blieve it's not quite solved... Either you should leave $current_page = $GET['page'] as it were (assuming things did work correctly in this regards before changes, or you should change each and every reference to $current_page, for example

            if(isset($_GET['page']) && $_GET['page'] > 0)
            {
            	/* If you do this, then your range is NOT 1 to count($pages),
            	 * but rather 0 to count($pages) - 1.
            	 */
            	$current_page = $_GET['page']-1;
            
            # which means you have to change here as well	
            if($current_page > count($pages) - 1)
            {
            	# and here
            	$current_page = count($pages) - 1;
            }
            }
            	else
            	{
            		# and here 
            		$current_page = 0;
            	}
            }
            
            # and from this point on, use $current_page rather than $current_page - 1
            
              Write a Reply...