2011-03-08 7 views
0

PHPを使用してレコードを簡単に上下に移動する方法はありますか?たとえば、レコードがこの順番(A、B、C、D)にある場合、ユーザーがレコード「C」の上ボタンをクリックすると、表示順序は(A、C、B、D)に変更する必要があります。 これは効率的です:PHPを使用してレコードを上下に移動

'C' -> 'temp'(copy) 
'C' -> 'B' (update) 
'B' -> 'temp' (update) 

これを行うには、任意のより良い方法はあります。

答えて

3

は、あなたは常にあなたがスワップし...リスト($ B、$ c)は=配列($ cと$ bを)したいeleentsの値を交換することができます。

<a href='?direction=dn&index=1'>Down</a> Item 1 

<a href='?direction=up&index=2'>Up</a><a href='?direction=dn&index=2'>Down</a> Item 2 

<a href='?direction=up&index=3'>Up</a><a href='?direction=dn&index=3'>Down</a> Item 3 

<a href='?direction=up&index=4'>Up</a> Item 4 

PHP側:

$direction=$_REQUEST['direction']; 
$index=$_REQUEST['index']; 

if($direction=='up'){ // if we clicked up then we want to swap with the preceeding item.. 
    list($items[$index-1],$items[$index])=array($items[$index],$items[$index-1]); 
}else{ // else we want to swap with the subsequent item. 
    list($items[$index+1],$items[$index])=array($items[$index],$items[$index+1]); 
} 
1

あなたの質問 "mysqlの" タグ付き。 ORDER BY SQL文を見てください。テーブルに "priority"という行を追加することもできます。各エントリは、昇順または降順で優先順位を取得します。ユーザーが注文を変更すると、2行を更新する必要があります。