SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 56 of 56
  1. #51
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if you still need it, but here's the Prev/Next class i use.

    PHP Code:
    <?

    // buildNav 1.1
    // Builds Prev/Next Class File
    //
    // Credits :
    //     Base on Original Code from Previous/next v.01 by sephiroth(alessandro@sephiroth.it)
    //     [url]http://www.sephiroth.it/file_detail.php?id=64[/url]
    //
    // Some Known Problems where the QUERY_STRING might be modified to reflect the values of $offset and $limit :
    //     - If $offset is modified unconditionally, it might lead to decimals for Prev/Next page numbers
    //     - If $limit is changed where offset is not 0, Prev/Next page number links will not reflect the current page number.
    //          Browse to offset=0 to reflect the page number changes
    //          This is ONLY noted when $db->limit = $limit where $limit is a global variable.
    //
    // Notable Issues :
    //     - If $offset is changed to a value higher then $this->total_result, the script will display the last entry ($this->total_result).
    //          show_info() will reflect this.
    //
    // General Comment Controls
    // -- Comments in Brackets [] denotes a Function or Class
    // -- Other Comments denote a General comment of a specific code
    // Copyright (c) 2002 Lyon Lim - For File Protocol 1.0
    // Contact Information : [url]http://nortiq.com[/url]
    //
    // Last Updated : 15th November 2002
    //
    // --------------------------------------------------------------

        
    class buildNav // [Class : Controls all Functions for Prev/Next Navigation Generation]
        
    {
            var 
    $offset// offset value
            
    var $offsetVar// offset variable name
            
    var $limit// controls the number of entries per page view
            
    var $sql_result// returns sql results
            
    var $num_pages// total number of pages from results

            
    function buildNav($offset$limit// [Default]
            
    {
                
    $this->limit $limit;
                
    $this->offset $_GET[$offset];
                
    $this->offsetVar $offset;
            }
            function 
    execute($query// [Function : mySQL Query Execution]
            
    {
                global 
    $systemDir;

                
    $this->offset = (!isset($this->offset)) ? $this->offset// assigning a default value of 0
                
    $this->sql_result mysql_query($query)
                                        or 
    error_log('read: ' mysql_error() . "\n",3,$systemDir '/error.log');
                
    $this->total_result mysql_num_rows($this->sql_result);
                if (isset(
    $this->limit)) { // if limit is specified, define the number of pages ($this->num_pages)
                    
    $query .= " LIMIT $this->offset,$this->limit";
                    
    $this->sql_result mysql_query($query)
                                            or 
    error_log('read: ' mysql_error() . "\n",3,$systemDir '/error.log');
                    
    $this->num_pages ceil($this->total_result/$this->limit);
                }
                if (
    $this->offset+$this->total_result) { // so $this->offset is never more or equal to $this->total_result. at least 1 less $this->total_result
                    
    $this->offset $this->total_result-1;
                }
            }

            function 
    show_num_pages($frew ''$rew 'prev'$ffwd ''$fwd 'next'$separator '|'$listClass ''// [Function : Generates Prev/Next Links]
            
    {
                
    $currentPg $this->offset/$this->limit+1// determining the current page
                
    if ($currentPg '5') {
                    
    $firstPg = ($currentPg-0) ? $currentPg-1;
                    
    $lastPg $currentPg+4;
                    if (
    $lastPg $this->num_pages) {
                        
    $lastPg $this->num_pages;
                        
    $firstPg = ($this->num_pages-0) ? $this->num_pages-1;
                    }
                }
                else {
                    
    $firstPg 1;
                    
    $lastPg = ($this->num_pages >= 10) ? 10 $this->num_pages// showing a maximum of 10 page links
                
    }

                if(
    $this->num_pages 1) {
                    
    // searching for http_get_vars
                    
    foreach ($_REQUEST as $queryName => $queryValue) {
                        if (
    is_array($_GET[$queryName])) {
                            foreach (
    $_GET[$queryName] as $queryArrayName => $queryArrayValue) {
                                
    $queryString .= '&' $queryName '[' $queryArrayName ']=' $queryArrayValue;
                            }
                        }
                        elseif (
    $queryName != $this->offsetVar) {
                            
    $queryString .= "&$queryName=$queryValue";
                        }
                    }
                    
    $listNext $this->offset+$this->limit;
                    
    $listPrev $this->offset-$this->limit;
                    
    $return .= ($this->offset 0) ? "[<a href=\"$_SERVER[PHP_SELF]?$this->offsetVar=0$queryString\" $listClass>$frew</a>] <a href=\"$_SERVER[PHP_SELF]?$this->offsetVar=$listPrev$queryString\" $listClass>$rew</a> $separator " "[$frew$rew $separator ";

                    
    // showing pages
                    
    for($pgCount=$firstPg$pgCount<=$lastPg$pgCount++) {
                        
    $offsetNext = ($pgCount-1)*$this->limit;
                        if (
    $offsetNext != $this->offset) { // that means user is not browsing at this offset
                            
    $return .= " <a href=\"$_SERVER[PHP_SELF]?$this->offsetVar=$offsetNext$queryString\" $listClass$pgCount</a> ";
                        }
                        else { 
    // user happens to be at this offset. so no link!
                            
    $return .= $pgCount;
                            
    $return .= ($pgCount $this->num_pages) ? $separator " ""// add a separator unless $pgCount is at the last page
                        
    }
                    }
                    
    $offsetNext $this->offset+$this->limit;
                    
    $offsetEnd = ($this->num_pages-1)*$this->limit;
                    
    $return .= ($this->offset $this->limit $this->total_result) ? "$separator <a href=\"$_SERVER[PHP_SELF]?$this->offsetVar=$offsetNext$queryString\" $listClass>$fwd</a> [<a href=\"$_SERVER[PHP_SELF]?$this->offsetVar=$offsetEnd$queryString\" $listClass>$ffwd</a>]" "$separator $fwd [$ffwd]";
                }
                return 
    $return;
            }

            function 
    show_info() // [Function : Showing the Information for the Offset]
            
    {
                
    $return .= $this->total_result ' Total Results<br>';
                
    $list_from = ($this->offset+$this->total_result) ? $this->total_result $this->offset+1;
                
    $list_to = ($this->offset+$this->limit >= $this->total_result) ? $this->total_result $this->offset+$this->limit;
                
    $return .= 'Showing Results from ' $list_from ' to ' $list_to '<br>';
                return 
    $return;
            }
        }
    ?>
    to run it, just include the class. eg.
    require_once("$path/buildNav.class.php");

    where offset is the variable name you use to define the offset position. eg in this case $offset
    and 15 is the limit for the results.
    $buildNav = new buildNav('offset','15');
    $buildNav->execute("[SQL STATEMENT]");
    $buildNav->show_num_pages('<','Previous','>','Next','');

    thats just an example you use to call the prev/next class. With just the declaration of a few other parameters and some modification to the code, you can have different formats for your Prev/Next class.

    be sure to check out the script this class was updated from.
    http://www.sephiroth.it/file_detail.php?id=64
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #52
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am wondering and I am not knocking anyone's work here by any means, but is it really a good idea to mix the database logic into the paging class? Both classes talked about here do it, I am thinking its probably better to take the query stuff out so one could port it to any RDBMS. What do you guys think?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #53
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Freddy,

    In my own work, I would normally abstract out the database by having the class work with ADODB, or another database abstraction layer. Is that what you had in mind, or were you thinking of going a step further and just passing the class a PHP array containing the result set?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #54
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both kind of, I was actually thinking to do a paging system you would really only need the total number of items, the current offset, limit and any url parameters that need to be passed to each page. That way you could use it with anything including directory listings, etc... It would be totally independent of what mechanism you use to get the data you want to page. What do you think?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #55
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It all depends what you want out of it. The code in my article, for example, encapsulates more than just the code to generate next/prev/page links. It actually encapsulates the result set so that the developer can use familiar techniques (e.g. looping through the set) and have the paging take care of itself.

    To build a completely generalized paging system that afforded the same kind of power, you'd need to develop a data binding interface like that provided by ASP.NET (where you can bind 'data sources' of any type to interface elements that support data binding). If you go that far, in my opinion, you're beginning to get beyond the scope of application for which PHP is the ideal solution.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #56
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The paged query class in Eclipse that I posted uses a several classes for the database that are separate from the pagedQuery class.

    Here's a snapshot of how it works:
    PHP Code:
     $query =& new PagedQuery($database->query($sql), 20); 
    What's going on there? $database (an Eclipse object) runs $sql (a query string) to produce a database result object. The paged query class then uses that object (which already has all the methods it needs). The 20 is the number of records per page.

    If that captures your interest check out the API documentation that explains it all better than I ever would.
    http://www.students.cs.uu.nl/people/...agedQuery.html
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •