Paging like google

Discussion in 'PHP' started by shopping, Jul 11, 2008.

  1. #1
    Hello..
    Can any one help me creating a dynamic pagination script for sql entries
    i made this simple paging script


    Code: ( text )

    it displays all the following:

    Select a Page
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Next->

    Can anyone help me to shorten the list of displaying page links? I want to do some like this:

    1 2 3 4 5 6 7 8 9 10 Next->

    and if the vistor is looking the page no 10 it should look like this

    <-Previous 11 12 13 14 15 16 17 18 Next->

    Thank's
     
    shopping, Jul 11, 2008 IP
  2. genetrix

    genetrix Banned

    Messages:
    400
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    its old script ... previously found at other site . did u copy paste it ?
     
    genetrix, Jul 11, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    You need to limit your input for example

    <?php
    // example count
    $pages = 20;
    
    // check if more pages then 10
    if ($pages > 10) {
        $toPages = 10;
    } elseif ($pages < 10) {
        $toPages = $pages;
    } else {
        $toPages = $pages;
    }
    
    // from position &from=page (7)
    if (isSet($_GET['from']) && is_numeric($_GET['from'])) {
        if ($_GET['from'] > $pages) {
             $from = $pages - 5;
        } elseif ($_GET['from'] < 0) {
             $from = 0;
        } else {
            $from = $_GET['from'];
        }
    } else {
        $from = 0;
    }
    
    if ($from - ($toPages/2) < 0)
    {
        $startPages = 1;
    }
    else
    {
        $startPages = $from - ($toPages/2);
    }
    
    echo '<a href="index.php">First</a>&nbsp;';
    for ($x = $startPages; $x < $startPages + $toPages; $x++)
    {
        echo '<a href="index.php?from=' . $x . '">';
        if ($x == $from) {
            // current page
            echo '<strong>' . $x . '</strong>';
        } else {
            // show page number
            echo $x;
        }
        echo '</a>&nbsp;';
    }
    
    
    echo '<a href="index.php?from=' . $pages . '">Last</a>';
    ?>
    PHP:
    Easy page navigation..
     
    EricBruggema, Jul 12, 2008 IP
  4. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try writing out algorithms on paper and convert them to pseudo code then php. That's how I learned.
     
    mlkshake, Jul 12, 2008 IP
  5. shopping

    shopping Peon

    Messages:
    260
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank's EricBruggema

    But I don't understand.. what did you mean?

    sorry!
     
    shopping, Jul 12, 2008 IP
  6. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    algorithms on paper
    == writing the basic logic on paper in your native language

    * like get number of records in data base
    * divide by records per page to get number pages
    * if current page more than 1 then show previous link and links from current page - 10 to current page that are more than 0
    * show links current page +1 to current page + 10 (but stop if u reach Max page)
    * show next link if there are more pages then current page

    so this is basic algorithm

    to make pseudo code
    write with line numbers and go to statements ...


    and convert them to pseudo code then php
     
    tgkprog, Jul 12, 2008 IP