Jump to content

Get next row


therealwesfoster

Recommended Posts

I have links that allow you to view info from different rows.

 

Setup like this:

[PREV ROW]

Current row info

[NEXT ROW]

 

My question is, how do I link the prev/next buttons to the next row's id? (page.php?id=)

 

They are in an auto-increment field, but alot of rows have been deleted, so using a simple current_id+1 will not work.

 

How do i do this?

Link to comment
https://forums.phpfreaks.com/topic/89052-get-next-row/
Share on other sites

You could Give this a try (may need to rename the variables, and table name though):

 

<?php
include 'db.php';
$currentID = $_GET['id'];
$sql = mysql_query("(SELECT id FROM snippets WHERE id = '$currentID' LIMIT 1)
UNION
(SELECT id FROM snippets WHERE id < '$currentID' ORDER BY id DESC LIMIT 1)
UNION
(SELECT id FROM snippets WHERE id > '$currentID' LIMIT 1)");
$i = 0;
while($row = mysql_fetch_array($sql)){
if($i == 1)
	echo 'Prev: '.$row['id'].'<br>';
if($i == 0)
	echo 'Curr: '.$row['id'].'<br>';
if($i == 2)
	echo 'Next: '.$row['id'].'<br>';
$i++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/89052-get-next-row/#findComment-456112
Share on other sites

You could Give this a try (may need to rename the variables, and table name though):

 

<?php
include 'db.php';
$currentID = $_GET['id'];
$sql = mysql_query("(SELECT id FROM snippets WHERE id = '$currentID' LIMIT 1)
UNION
(SELECT id FROM snippets WHERE id < '$currentID' ORDER BY id DESC LIMIT 1)
UNION
(SELECT id FROM snippets WHERE id > '$currentID' LIMIT 1)");
$i = 0;
while($row = mysql_fetch_array($sql)){
if($i == 1)
	echo 'Prev: '.$row['id'].'<br>';
if($i == 0)
	echo 'Curr: '.$row['id'].'<br>';
if($i == 2)
	echo 'Next: '.$row['id'].'<br>';
$i++;
}
?>

 

Fancy.. I'll let you know how that goes later today :)

Link to comment
https://forums.phpfreaks.com/topic/89052-get-next-row/#findComment-456134
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.