SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP Pagedresults Page Navigation

    Im using the pagedresults.php that can be found in an article on SitePoint by Kevin Yank.

    The file pagedresults.php looks like this:
    PHP Code:
    <?php

    class MySQLPagedResultSet
    {

      var 
    $results;
      var 
    $pageSize;
      var 
    $page;
      var 
    $row;
      
      function 
    MySQLPagedResultSet($query,$pageSize,$cnx)
      {
        
    $resultpage $_GET['resultpage'];
        
        
    $this->results = @mysql_query($query,$cnx);
        
    $this->pageSize $pageSize;
        if ((int)
    $resultpage <= 0$resultpage 1;
        if (
    $resultpage $this->getNumPages())
          
    $resultpage $this->getNumPages();
        
    $this->setPageNum($resultpage);
      }
      
      function 
    getNumPages()
      {
        if (!
    $this->results) return FALSE;
        
        return 
    ceil(mysql_num_rows($this->results) /
                    (float)
    $this->pageSize);
      }
      
      function 
    setPageNum($pageNum)
      {
        if (
    $pageNum $this->getNumPages() or
            
    $pageNum <= 0) return FALSE;
      
        
    $this->page $pageNum;
        
    $this->row 0;
        
    mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
      }
      
      function 
    getPageNum()
      {
        return 
    $this->page;
      }
      
      function 
    isLastPage()
      {
        return (
    $this->page >= $this->getNumPages());
      }
      
      function 
    isFirstPage()
      {
        return (
    $this->page <= 1);
      }
      
      function 
    fetchArray()
      {
        if (!
    $this->results) return FALSE;
        if (
    $this->row >= $this->pageSize) return FALSE;
        
    $this->row++;
        return 
    mysql_fetch_array($this->results);
      }
      
      function 
    getPageNav($queryvars '')
      {
        
    $nav '';
        if (!
    $this->isFirstPage())
        {
          
    $nav .= "<a href=%22sections.php?name=ViewCat&resultpage=&quot;.
                  (
    $this->getPageNum()-1).'&'.$queryvars.'">Prev</a';
        }
        if ($this->getNumPages() > 1)
          for ($i=1; $i<=$this->getNumPages(); $i++)
          {
            if ($i==$this->page)
              $nav .= "$i ";
            else
              $nav .= "<a href=%22sections.php?name=ViewCat&resultpage={$i}&&quot;.
                      $queryvars."\">{$i}</a> ";
          }
        if (!$this->isLastPage())
        {
          $nav .= "<a href=%22sections.php?name=ViewCat&resultpage=&quot;.
                  ($this->getPageNum()+1).'
    &'.$queryvars.'">Next</a> ';
        }
        
        return 
    $nav;
      }
    }

    ?>
    The code in question would be the function getPageNav:
    PHP Code:
    function getPageNav($queryvars '')
      {
        
    $nav '';
        if (!
    $this->isFirstPage())
        {
          
    $nav .= "<a href=%22sections.php?name=ViewCat&resultpage=&quot;.
                  (
    $this->getPageNum()-1).'&'.$queryvars.'">Prev</a';
        }
        if ($this->getNumPages() > 1)
          for ($i=1; $i<=$this->getNumPages(); $i++)
          {
            if ($i==$this->page)
              $nav .= "$i ";
            else
              $nav .= "<a href=%22sections.php?name=ViewCat&resultpage={$i}&&quot;.
                      $queryvars."\">{$i}</a> ";
          }
        if (!$this->isLastPage())
        {
          $nav .= "<a href=%22sections.php?name=ViewCat&resultpage=&quot;.
                  ($this->getPageNum()+1).'
    &'.$queryvars.'">Next</a> ';
        }
        
        return 
    $nav;
      } 
    The problem with this function is, it is not limited at all. Say there were 50 pages in the result set, it would display:

    If your on page 4:
    Prev 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21...etc

    What I would like to change in the file, which i dont know how, would make it output only 4 pages in each direction:

    If your on Page 4:
    Prev 1 2 3 4 5 6 7 8 Next

    _______________

    Someone before told me to add this to the function:
    PHP Code:
     $max_pages $this->getNumPages();
    $num_pages 4;  //<== this is the number of links you want before and after the current page. Change as required
    $curr_page $this->page;

    $start_page = ($curr_page $num_pages) ? $curr_page $num_pages;
    $end_page = ($max_pages < ($curr_page $num_pages) ) ? $max_page $curr_page $num_pages
    And to change the for loop to this:
    PHP Code:
    for ($i $start_page$i <= $end_page$i++) 
    But it didnt seem to work, also he didnt inform me on where to place the first set of code into the function, it was a random guess on my part. If this code above is the right code, could someone tip me off on to where in the code i need to place it??

    Thanks,

    BKerr
    BKerr

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help?
    BKerr

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please?
    BKerr


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
  •