SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I show result like this....HELP

    Hello,

    I have a page that performs a query and then displays 5 records on a page. All five records' id is being passed as url variable as id and calls up:
    test.php?id=

    on Test.php I pick up the id like;
    $id = (int) $_Get['id'];

    I am able to display the $id but now I would like to have NEXT | PREV links so user can go navigate through the records. I have tried many pagination scripts and tutorials nothing seems to work fully.

    So if anyone can point me into the right direction, I would highly appreciate it.

  2. #2
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Brisbane, Australia
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first thing your script needs to know, is what records it needs to show (1 - 5, 6 - 10, 11 - 15, et cetera).

    When you are absolutely sure that your script knows that, you can then make a simple change to your query to retreive only the records you want:

    PHP Code:
    $query 'SELECT myField FROM myTable WHERE ' $where_clause ' LIMIT ' $start_record ',' $number_of_records ';'
    The above is a generic thing you could use. A practical example showing records 6 - 10 with no WHERE clause, is the following:
    PHP Code:
    $query 'SELECT myField FROM myTable LIMIT 5,5'
    And then records 11 - 15...
    PHP Code:
    $query 'SELECT myField FROM myTable LIMIT 10,5'
    Remember that the starting record is a zero-based array.

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since database IDs are non-consecutive, this is a problem. If you want to iterate through the ordered IDs, however, here's a neat trick I saw once:
    Form your NEXT URL with the current item's ID and another parameter, e.g. dir(ection):
    PHP Code:
    echo "<a href=\"disp.php?id=$id&amp;dir=next\">NEXT</a>"
    (PREV would be the same thing only with "prev" instead of "next" in the URL.)
    Then when you run your query check for the presence of the GET parameter "dir":
    PHP Code:
    $sql "SELECT id,name FROM table";
    if(!isset(
    $_GET['dir']))
        
    $sql .= " WHERE id=$id";
    else
        if(
    $_GET['dir']=="next")
            
    $sql .= " WHERE id>$id ORDER BY id ASC LIMIT 1";
        else 
    //dir=="prev"
            
    $sql .= " WHERE id<$id ORDER BY id DESC LIMIT 1"
    The above code snippet will construct a query that, based solely on an item's ID, will fetch either the next or previous item (when ordered by ID). I've always thought this was kinda slick, hope it works for you.
    PHP questions? RTFM
    MySQL questions? RTFM

  4. #4
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks you so much for your help

    Thanks TheAnarchist.
    I have been pulling my hair out for the past week trying to resolve this issue.
    Kromey, you are a fountain of knowledge.

    Thanks a bunch.

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ultrajeeps View Post
    Kromey, you are a fountain of knowledge.
    Aw, shucks! And to think, I read this right after I post in the "What's the best professional compliment you've received?" thread!
    PHP questions? RTFM
    MySQL questions? RTFM

  6. #6
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still stuck

    I have put this code together but still doesn't work. Can you tell me what am I doing wrong? Thanks.

    PHP Code:
    <?php

    $dbhost        
    =        'localhost';
    $dbuser        =        'testuser';
    $dbpass        =        'testpass';
    $dbname        =        'testdb';
    $tblname    =        'testusers';

    $conn mysql_connect($dbhost$dbuser$dbpass) or die (mysql_error('CANNOT CONNECT'));
    $db mysql_select_db($dbname$conn) or die (mysql_error('CANNOT CONNECT TO DB'));

    $id = (int) $_GET['id'];


    $query "SELECT * FROM $tblname WHERE user_id = $id";
    $result mysql_query($query);
    $rows mysql_fetch_array($result);


    echo 
    $rows['user_id'];

    echo 
    "<a href=\"test.php?id=$id&amp;dir=next\">next</a>";     

    $sql "SELECT user_id FROM $tblname";
    if(!isset(
    $_GET['dir']))
        
    $sql .= " WHERE user_id=$id";
    else
        if(
    $_GET['dir']=="next")
            
    $sql .= " WHERE user_id > $id ORDER BY user_id ASC LIMIT 1";
        else 
    //dir=="prev"
            
    $sql .= " WHERE user_id < $id ORDER BY user_id DESC LIMIT 1";

  7. #7
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ultrajeeps View Post
    I have put this code together but still doesn't work. Can you tell me what am I doing wrong? Thanks.
    ultrajeeps, please follow this link I've posted the pagination to your query.
    You have to understand how pagination works.
    my mobile portal
    ghiris.ro


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
  •