Getting next and previous row in a MySQL set!

Reply

Join Date: Feb 2005
Posts: 1
Reputation: Prorgamme is an unknown quantity at this point 
Solved Threads: 0
Prorgamme Prorgamme is offline Offline
Newbie Poster

Getting next and previous row in a MySQL set!

 
0
  #1
Feb 20th, 2005
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??
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 50
Reputation: barnamos is an unknown quantity at this point 
Solved Threads: 0
barnamos's Avatar
barnamos barnamos is offline Offline
Junior Poster in Training

Re: Getting next and previous row in a MySQL set!

 
0
  #2
Mar 24th, 2005
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; ?>

<a href="?row_num=<? echo $goin_back;?>">Back</a> ||
<a href="?row_num=<? echo $goin_ahead;?>">Forward</a>

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

Make sense?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC