944,045 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 16482
  • PHP RSS
Feb 20th, 2005
0

Getting next and previous row in a MySQL set!

Expand Post »
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??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Prorgamme is offline Offline
1 posts
since Feb 2005
Mar 24th, 2005
0

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

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?
Reputation Points: 15
Solved Threads: 0
Junior Poster in Training
barnamos is offline Offline
50 posts
since Mar 2005
Oct 15th, 2010
0
Re: Getting next and previous row in a MySQL set!
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hourglass is offline Offline
1 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Passing array through in url
Next Thread in PHP Forum Timeline: insert multiple values





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC