Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Page script

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Page script

    Im using the standard page script in php:

    PHP Code:

    <?php
        
        
    require("db.php");

            
    db_connect();
            

        
    $max_results 2;

    if(!isset(
    $_GET['page'])){
        
    $page 1;
    } else {
        
    $page $_GET['page'];
    }

    $from = (($page $max_results) - $max_results); 

        
            
    $sql mysql_query("SELECT id, uid, title, category_main, category_sub, description FROM `tutorials` LIMIT $from, $max_results") or die("MySQL Error"); 

    while(
    $row mysql_fetch_array($sql))
    {
    echo( 
    "<b>$row[title]</b><br>" );
    echo( 
    "    submitted by $row[uid] in $row[category_main] / $row[category_sub]<br>" );
    echo( 
    "<i>$row[description]</i><br><br>" );

    }

    $total_results mysql_result(mysql_query("select count(*) as Num FROM tutorials"),0);

    $total_pages ceil($total_results $max_results);

     if((
    $total_pages) < 2){ echo "..."; }
     else{

    echo 
    "Page <b>$page</b> out of <b>$total_pages</b><br />";

    if(
    $page 1){
        
    $prev = ($page 1);
        echo 
    "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
    }

    for(
    $i 1$i <= $total_pages$i++){
        if((
    $page) == $i){
            echo 
    "$i ";
            } else {
                echo 
    "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
        }
    }

    if(
    $page $total_pages){
        
    $next = ($page 1);
        echo 
    "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
    }

    }
                

            
    db_disconnect();
        
        
    ?>
    but what im trying to do the effect where it will display 5 page numbers at a time only the dot dot dot to the last and first two pages...

    Example:



    it basicly displays the page your on then 2 pages ahead... 2 pages behind... the first 2 pages and last two pages....

    Thanks

  • #2
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, pages 1 and 2 are easy...they'll always be there as long as you have at least 2 pages.

    For the others, just do some simple math to determine which buttons to display. $total_pages gives you the last number, $total_pages - 1 gives you the next to last.

    $current_page gives you the current page...-1, -2....+1, +2.
    If ($current_page -1) > 3 then put in the first set of dots.
    If ($current_page -1) < ($total_pages -2) then put in second set of dots.

    Does that cover what you were trying to figure out, or do you want actual code? Looks like you know how to write the code though.


  •  

    Posting Permissions

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