Results 1 to 10 of 10

Thread: Pagination

Threaded View

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved Pagination

    I've found the below code and modified it to my needs and everything is displayed the way I want it. (well nearly!) The pagination looks OK if there are no more than 4/5 pages (eg Page 3 of 5 < 1 2 3 4 5 >) however if there are a large number of pages I don't want it to look like (Page 7 of 10 < 1 2 3 4 5 6 7 8 9 10 > ) but rather like (Page 7 of 10 < 6 7 8 9 10 >)

    Hopefully I've explained myself clear enough
    PHP Code:
    <?php 

        
    @mysql_connect($localhost$user$password) or die("ERROR--CAN'T CONNECT TO SERVER"); 
        @
    mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); 

        
    $limit          25;                
        
    $query_count    "SELECT count(*) FROM table";     
        
    $result_count   mysql_query($query_count);     
        
    $totalrows      mysql_num_rows($result_count); 

        if(empty(
    $page)){ 
            
    $page 1
        } 
             

        
    $limitvalue $page $limit - ($limit); 
        
    $query  "SELECT * FROM table LIMIT $limitvalue$limit";         
        
    $result mysql_query($query) or die("Error: " mysql_error()); 

        if(
    mysql_num_rows($result) == 0){ 
            echo(
    "Nothing to Display!"); 
        } 

        
    $bgcolor "#E0E0E0"// light gray 

        
    echo("<table>"); 
         
        while(
    $row mysql_fetch_array($result)){ 
            if (
    $bgcolor == "#E0E0E0"){ 
                
    $bgcolor "#FFFFFF"
            }else{ 
                
    $bgcolor "#E0E0E0"
            } 

        echo(
    "<tr bgcolor=".$bgcolor.">n<td>"); 
        echo(
    $row["users"]); 
        echo(
    "</td>n<td>"); 
        echo(
    $row["usersID"]); 
        echo(
    "</td>n</tr>"); 
        } 

        echo(
    "</table>"); 

        if(
    $page != 1){ 
            
    $pageprev $page--; 
             
            echo(
    "<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
        }else{ 
            echo(
    "PREV".$limit." "); 
        } 

        
    $numofpages $totalrows $limit
         
        for(
    $i 1$i <= $numofpages$i++){ 
            if(
    $i == $page){ 
                echo(
    $i." "); 
            }else{ 
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
            } 
        } 


        if((
    $totalrows $limit) != 0){ 
            if(
    $i == $page){ 
                echo(
    $i." "); 
            }else{ 
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
            } 
        } 

        if((
    $totalrows - ($limit $page)) > 0){ 
            
    $pagenext $page++; 
              
            echo(
    "<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
        }else{ 
            echo(
    "NEXT".$limit); 
        } 
         
        
    mysql_free_result($result); 

    ?>
    Last edited by lintz; Oct 5th, 2005 at 10:02 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width