User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 397,770 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,531 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 7007 | Replies: 1
Reply
Join Date: Feb 2005
Posts: 1
Reputation: Prorgamme is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Prorgamme Prorgamme is offline Offline
Newbie Poster

Getting next and previous row in a MySQL set!

  #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??
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Location: Colorado
Posts: 50
Reputation: barnamos is an unknown quantity at this point 
Rep Power: 4
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!

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC