473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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;
}


Jul 17 '05 #1
5 12456
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" <vf*****@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;
}


Jul 17 '05 #2
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.
Jul 17 '05 #3
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.
Jul 17 '05 #4
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.
Jul 17 '05 #5
On Sun, 9 Nov 2003 15:58:22 +0000 (UTC), Disco Plumber <sc**@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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working...
5
by: Bob Bedford | last post by:
Does exist a next prev function in PHP/Mysql. I've a website with returns lot of records (say about 200). The user may browse trough the list or returned records, but he has to click the record...
2
by: eeh | last post by:
Hi, Suppose I have a table "product" with fields id,name,price. I want to get a resultset of previous and current and next record when the resultset is sorted by id and id=10. The table data is...
1
by: ubmc | last post by:
Revised form layout by dragging/dropping fields from field list and deleting unwanted fields. However, unable to go to next existing record or return to previous existing record, but can add new...
9
by: Karl Roes | last post by:
Hi All, I would like some advice on Next / Previous record buttons. I have a main form for the client, and a continuous subform listing client transactions. If I open one of these transactions...
15
by: Susan Bricker | last post by:
Greetings. I have a Mainform with a subform. The Mainform has single record format and subform has continuous form format. When the Mainform opens, I force allowadditions and allowedits to FALSE...
1
by: David | last post by:
What I envision is this. The user gets a set of records in a data grid Then the user gets to select one of the rows to view the detail information about the record. Then instead of backing...
1
by: cbanks | last post by:
Hello Ladies/Gents, im in need of some serious help here.. I have a tool that allows users to view information on a form and make changes to recordsets. This tool has buttons that allow a user to...
13
by: sweetline priya | last post by:
in my project, a table called 'hardware' contains merely 5000 records..its a mysql database.. the user can add, modify or can view this hardware details one by one.. while clicking 'view' button, the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.