Next and Previous record

This is a discussion on Next and Previous record within the PHP Language forums, part of the PHP Programming Forums category; I am moving throught a set of records, my next works great but previous move to the first record. Also ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-08-2003
Rick
 
Posts: n/a
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;
}






Reply With Quote
  #2 (permalink)  
Old 11-09-2003
Rick
 
Posts: n/a
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" <vfrrick@cox.net> wrote in message
news:xmerb.7185$Zb7.6789@fed1read01...
> 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;
> }
>
>
>
>
>
>



Reply With Quote
  #3 (permalink)  
Old 11-09-2003
Disco Plumber
 
Posts: n/a
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.
Reply With Quote
  #4 (permalink)  
Old 11-09-2003
Jerry Gitomer
 
Posts: n/a
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.
Reply With Quote
  #5 (permalink)  
Old 11-09-2003
Disco Plumber
 
Posts: n/a
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.
Reply With Quote
  #6 (permalink)  
Old 11-09-2003
Andy Hassall
 
Posts: n/a
Default Re: Next and Previous record

On Sun, 9 Nov 2003 15:58:22 +0000 (UTC), Disco Plumber <scag@moralminority.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 (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0