SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am a frequent reader but infrequent poster on this forum. I have learned a lot and solved a lot of problems due to posts here. So as a return I want to contribute something. Hopefully it will be helpfull to someone. Next/Prev links seem to be a very common question and hang up for php programmers when they first start out, so here is the basic code that I us to do next/previous links. It works well. Most of it I have adapted from other sources and modified over time to come to this end result. I really don't know where some of the original concepts came from. Of course you will want to edit the html to your liking. Enjoy!!

    PHP Code:
    <?
    //First we tell it how many rows of date to return
    $limit=10;

    // next determine if offset has been passed to script, if not use we assign it a value of 0
    if (empty($offset)) {
        
    $offset=0;
    }

    //determine how many total rows are in our data set
    $result mysql_query("select count(A COLUMN NAME) ".
                            
    "from TABLE_NAME ".
                                
    "where WHATEVER ".
                                    
    "orderby Whatever"); 
    $numrows mysql_result($result00); 

    //get results
    $result=mysql_query("select id,name,phone ".
                            
    "from TABLE where YOUR CONDITIONAL HERE ".
                                
    "order by WHATEVER ".
                                    
    "limit $offset,$limit");

    //now you can display the results returned
    while ($data=mysql_fetch_array($result)) {
        
    //include code to display results as you see fit
    }

    //calculate number of pages needing links
    $pages=intval($numrows/$limit);
            
    //detemine if we need to add a page for a last page with less than the full number of rows
    if ($numrows%$limit) {
        
    //If there was a remainder we add a row
        
    $pages++;
    }

    //set up variable equal to the record number of the first displayed on current page        
    $first_record $offset 1;

    //start displaying the Next/Previous links
    echo "Now viewing: $first_record -";

    //Check to see if it the last page
    if (!((($offset)/$limit)+1==$pages)) {
        
    //not last page so give so last record on page is $offset + $limit
        
    $last_record $offset $limit;
        echo 
    $last_record of $numrows items &nbsp; &nbsp; ";
    } else {
        
    //is the last page so last record on page is $numrows
        
    echo $numrows of $numrows item(s) &nbsp; &nbsp; ";
    }
                
    //next we need to do the links to other results
    if ($offset != 0) { //bypass PREV link if offset is 0
        
    $prevoffset=$offset-$limit;
        echo 
    "<a href=\"$PHP_SELF?offset=$prevoffset\">Prev</a> &nbsp; \n";
    }

    //if there is only 1 page we need no numbered links.        
    if ( $pages != ) {
        for (
    $i=1;$i<=$pages;$i++) { //loop thru when more than one page
            
    $newoffset=$limit*($i-1);
            
    //echo the number of the currently viewed page without a hyperlink
            
    if ( ((($offset)/$limit)==($i-1)) ) {
                echo 
    "$i &nbsp; \n";
            } else {
                echo 
    "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> &nbsp; \n";
            }
        }
    }
            
    //check to see if last page
    if (!((($offset)/$limit)+1==$pages) && $pages!=1) {
        
    //not last page so give NEXT link
        
    $newoffset=$offset+$limit;
        echo 
    "<a href=\"$PHP_SELF?offset=$newoffset\">Next</a><p>\n";
    }
    ?>
    Last edited by Marty H.; Apr 13, 2001 at 10:30.
    Marty H.

  2. #2
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Marty

    Thanks ... I think this script will be very helpfull to me...... especially when I just starting to pick u php
    Ngu I.P.
    Web Developer

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code Marty. I haven't read through it, but I mad a copy. Yes, this is a popular topic.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    marty, i'm sure that a lot of people will find the script helpfull. thank you.

    i could have used it a couple of months ago when i was dying to figure the next/previous thing out. lucky for me a certain cat *ahemfreddy*cough* was there to help.
    . . . chris


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
  •