I am trying to make a function in my db driven site where I can move articles up & down on a page. Here's my article table..

sort_id | page_id | title | text

This way I can auto increment the sort_id (not a primary key) so every new article gets a new number. I did look at ways that would +1 or -1 from the sort_id, but since the prev or next article may be any number higher or lower, the only way I can be sure they will swap place is if they physically swap sort_id.

So I have a foreach loop going something like this..
PHP Code:
foreach(articles as article){
$title.' <a href=move.php?sort_id='.$article['sort_id'].'&direction=up>Move up</a> |<a href=move.php?sort_id='.$article['sort_id'].'&direction=down>Move down</a>';

So now if I click move up, how can I capture the value of the previous iteration so that I can add it to the query string to make the swap happen. Or am I just barking up the wrong tree here?