SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Auto pagination for long text fields

    If you have a really long text field in mysql(longtext) and lets say you want to pagination every 40 sentences(or every so long). How can you do this with code.

    I know how to do pagination like if you want to have only 15 mysql entries per page, but I don't know how to do it if its just one field.

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use something like...

    PHP Code:
    class Page
     
    {
     var 
    $num;
     var 
    $text;
     var 
    $sentences;
     
     var 
    $i;
     var 
    $perpage;
     
         function 
    Page($text$pagenum=1$perpage=40)
         {
             
    $this->num $pagenum;
             
    $this->perpage $perpage;
             
    $this->text $text;
     
             
    $this->start $this->perpage * ($this->num 1); // 40*(1-1)=0, 40*(2-1)=40. This is the starting point
             
    $this->stop $this->perpage $this->num// 40*1=40, 40*2=80, this is the ending point
     
             
    $this->sentences();
         }
     
         function 
    sentences()
         {
             
    $this->sentences explode('. '$this->text); // Use ". " instead of just "." to avoid issues with elipses.
             
    $this->sentences array_filter($this->sentences, array($this'filter'));
         }
     
         function 
    filter($in)
         {
             if (
    $this->>= $this->start && $this->$this->stop)
             {
                 
    $r true// Leave it in the array.
             
    }
             else
             {
                 
    $r false;
             }
             
    $this->i++; // Increment for next time.
             
    return $r;
         }
     
         function 
    output()
         {
             echo 
    implode('. '$this->sentences).'.';
         }
     }
     
     
    $str 'Hello. World. I. Am. Testing.';
     
     
    $p = new Page($str12);
     
    $p->output();
     echo 
    '<br/><br/>';
     
     
    $p = new Page($str22);
     
    $p->output();
     echo 
    '<br/><br/>';
     
     
    $p = new Page($str32);
     
    $p->output();
     echo 
    '<br/><br/>'


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
  •