Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.