Professional Web Applications Themes

Next and Previous record - PHP Development

I am moving throught a set of records, my next works great but previous move to the first record. Also what is the best way to check and see if it is the first or last record in the set. if($move == "Next") { $next_rec = "select * from people where contactid > $rec_id limit 1"; $results = mysql_query($next_rec); $row = mysql_fetch_row($results); include("http://localhost/keith-homes/hyperlink.php?val=$row[0]"); exit; } elseif($move == "Previous") { $next_rec = "select * from people where contactid < $rec_id limit 1"; $results = mysql_query($next_rec); $row = mysql_fetch_row($results); include("http://localhost/keith-homes/hyperlink.php?val=$row[0]"); exit; }...

  1. #1

    Default Next and Previous record

    I am moving throught a set of records, my next works great but previous move
    to the first record. Also what is the best way to check and see if it is the
    first or last record in the set.

    if($move == "Next")
    {
    $next_rec = "select * from people where contactid > $rec_id limit 1";
    $results = mysql_query($next_rec);
    $row = mysql_fetch_row($results);
    include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
    exit;
    }
    elseif($move == "Previous")
    {
    $next_rec = "select * from people where contactid < $rec_id limit 1";
    $results = mysql_query($next_rec);
    $row = mysql_fetch_row($results);
    include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
    exit;
    }






    Rick Guest

  2. #2

    Default Re: Next and Previous record

    I understand why it goes to the first record and would like a better way to
    navigate backwards through the set. It starts it query at the first record
    and the first is less than the current ID so it stops there. Correct.


    "Rick" <vfrrickcox.net> wrote in message
    news:xmerb.7185$Zb7.6789fed1read01...
    > I am moving throught a set of records, my next works great but previous
    move
    > to the first record. Also what is the best way to check and see if it is
    the
    > first or last record in the set.
    >
    > if($move == "Next")
    > {
    > $next_rec = "select * from people where contactid > $rec_id limit 1";
    > $results = mysql_query($next_rec);
    > $row = mysql_fetch_row($results);
    > include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
    > exit;
    > }
    > elseif($move == "Previous")
    > {
    > $next_rec = "select * from people where contactid < $rec_id limit 1";
    > $results = mysql_query($next_rec);
    > $row = mysql_fetch_row($results);
    > include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
    > exit;
    > }
    >
    >
    >
    >
    >
    >

    Rick Guest

  3. #3

    Default Re: Next and Previous record

    Mr. Clean, speaking through Rick, wrote:
    >
    > $next_rec = "select * from people where contactid > $rec_id limit 1";
    try:

    SELECT * FROM people
    WHERE contactid > $rec_id
    ORDER BY contactid
    LIMIT 1
    > $next_rec = "select * from people where contactid < $rec_id limit 1";
    SELECT * FROM people
    WHERE contactid < $rec_id
    ORDER BY contactid DESC
    LIMIT 1

    /joe
    --
    In lbbs, El Scorcho's plain radio from Robert Jameson is blue?? Krispy
    Kreme is retarded.
    Disco Plumber Guest

  4. #4

    Default Re: Next and Previous record

    On Sat, 08 Nov 2003 16:10:15 -0700, Rick wrote:
    > I understand why it goes to the first record and would like a better way
    > to navigate backwards through the set. It starts it query at the first
    > record and the first is less than the current ID so it stops there.
    > Correct.
    >
    >
    You are correct and, if rec_id is the lead column in an index it returns
    that first record very, very quickly.

    Depending on which RDBMS product you are using Joe's suggestion to use
    ORDER BY DESC may not work. The reason is that some RDBMS do the ORDER BY
    as the final step in the retrieval and presentation process. If it works
    with the RDBMS you are using great. If it doesn't look into using a
    subquery whose inner query retrieves the records in descending sequence
    with an outer query that does your select less than.
    Jerry Gitomer Guest

  5. #5

    Default Re: Next and Previous record

    Jerry Gitomer, obviously a huge fan of Mean Ween, wrote:
    >
    > Depending on which RDBMS product you are using Joe's suggestion to use
    > ORDER BY DESC may not work.
    I should offer the caveat that all of my suggestions only refer to
    MySQL, which is the only one I use.
    > The reason is that some RDBMS do the ORDER BY as the final step in the
    > retrieval and presentation process.
    I don't understand this. Are you saying that some products do the LIMIT 1
    before the ORDER DESC? That is ridiculously poor functionality. In such
    a system, would the only way to do it (without using PHP arrays/sorting)
    be to do a subquery as you suggested? That is terribly inconvenient.

    Do you know any specific database products offhand which do this?

    /joe
    --
    Brian McNamara's terrorist cell from gtpj is nauseous and educational in
    the Righteous Room. Miotch's processor is blue. David Maynor emasculates
    the non-descript memory and the network cable from matlab, and then
    interestingly loathes the masturbatory mp3 collection from Sarah Moore.
    Disco Plumber Guest

  6. #6

    Default Re: Next and Previous record

    On Sun, 9 Nov 2003 15:58:22 +0000 (UTC), Disco Plumber <scagmoralminority.org>
    wrote:
    >> The reason is that some RDBMS do the ORDER BY as the final step in the
    >> retrieval and presentation process.
    >
    >I don't understand this. Are you saying that some products do the LIMIT 1
    >before the ORDER DESC? That is ridiculously poor functionality. In such
    >a system, would the only way to do it (without using PHP arrays/sorting)
    >be to do a subquery as you suggested? That is terribly inconvenient.
    >
    >Do you know any specific database products offhand which do this?
    The closest I know of is Oracle's ROWNUM pseudocolumn, which is assigned
    before the ORDER BY, so if you naively use this to emulate LIMIT you'll get the
    wrong results.

    But ROWNUM is not really the same as LIMIT in the first place so probably not
    a fair comparison.

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

Similar Threads

  1. Record Set & Next | Previous Links
    By drdagwood in forum Coldfusion - Getting Started
    Replies: 0
    Last Post: October 14th, 12:08 AM
  2. Replies: 2
    Last Post: December 17th, 03:58 PM
  3. Copy value from previous record?
    By Steve in forum FileMaker
    Replies: 1
    Last Post: August 6th, 10:06 PM
  4. Fill a field from the previous record
    By Bruce Rodtnick in forum Microsoft Access
    Replies: 3
    Last Post: August 1st, 11:11 PM
  5. Stop adding record in subform after record count = 1
    By Charlie in forum Microsoft Access
    Replies: 0
    Last Post: July 31st, 10:42 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139