Results 1 to 10 of 10

Thread: Pagination

  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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Pagination

    Have you seen the pagination example here? The Hobo wrote a function which generates the links and it works very well.

    http://www.vbforums.com/showthread.p...51#post1988751
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

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

    Re: Pagination

    I tried that code but got an error when the code got to $db = new SiteDb; and I don't follow this part either

    PHP Code:
    if (! $result $db->query($query)) { 
            echo(
    $db->error()); 
            die(
    'Query error.'); 
        } 
    which is why I went searching for another example.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Pagination

    You just need to replace those functions with the equivilent mysql functions. So,

    $db = new SiteDb -----> mysql_connect()
    $db->query() ----------> mysql_query()
    $db->fetch_assoc() ----> mysql_fetch_assoc()
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

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

    Re: Pagination

    I'll give it a shot

    Does the example The Hobo wrote do what my original post ask?


    I see you've changed the example
    Last edited by lintz; Oct 5th, 2005 at 01:35 AM.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Pagination

    Did you have a look at the link in the post. You can see exactly what it does:

    http://adam.codedv.com/examples/pagination.php
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

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

    Re: Pagination

    my mistake

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Pagination

    Quote Originally Posted by lintz
    I'll give it a shot

    Does the example The Hobo wrote do what my original post ask?


    I see you've changed the example
    I should have edited it before I posted it.

    The DB lcass is just a wrapper for the MySql functions. But as it wraps the functions you can also make one for Oracle, ODBC and other packages and just switch the object when required
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

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

    Re: Pagination

    I can't get Hobo's code to work.

    Here is my situation.
    TotalRecords = 32
    ItemsPerPage = 10
    PagesToShow per pagination = 3

    Overall there are 4 pages to display.

    Page 1 works OK. (Page 1 of 4 << < 1 2 3 > >> )

    Page 2 doesn't as it shows page 4 when it shouldn't (Page 2 of 4 << < 1 2 3 4 > >> )

    Page 3 works OK. (Page 3 of 4 << < 2 3 4 > >> )

    Page 4 doesn't as it deosn't show page 2 (Page 4 of 4 << < 3 4 > >>)

    Any suggestions?

  10. #10

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

    Re: Pagination

    I modified Hobos code and it now works the way it should

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