954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting next and previous row in a MySQL set!

I have this simple script:

[PHP]<?php

// MySQL Connection
mysql_connect($sql['host'],$sql['user'],$sql['pass']) or die("Unable to connect to SQL server");
mysql_select_db($sql['data']) or die("Unable to find DB");

// Getting rows
$select = "SELECT * FROM table WHERE uid = '". $_POST['uid'] ."'";
$data = mysql_db_query($sql['data'], $select) or die("Select Failed!");

// Query results
$row = mysql_fetch_array($data);
$id = $row['uid'];
$type = $row['type'];
print("$id - $type");

?>[/PHP]

This is the simple version. The table has alot of items all having an uid number. Also a type is choosen for each item. When viewing an item i want an 'next' and 'previous' link going to the next item and previous item for the selected type.

Let say i have 5 item with the type "Type1". The 5 items will have the uid numbers: 1, 5, 8, 14 and 15. I want to make a 'next' and 'previous' link, that links to the respective item.

Is there a simple way the get the result from MySQL and is there a simple way to go to the next result and the prevois result??

Prorgamme
Newbie Poster
1 post since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Pass the current row count as a variable to the query.
At each result row add
<? $goin_back = $row_num - 1;
$goin_ahead = $row_num + 1; ?>

Back ||
Forward

Then your query does a:
SELECT * FROM table LIMIT $row_num , 1

Make sense?

barnamos
Junior Poster in Training
50 posts since Mar 2005
Reputation Points: 15
Solved Threads: 0
 

Hellou can you help me please with this.
All is going right but when i am on the end of mysql it didn`t show me anything.
Can you tell me way how to hide next or preview link when next or preview row doesn`t exist?
Or how to stay on row ?

thank you for reply.

Hourglass
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You