开发者

Move the records up and down using PHP

开发者 https://www.devze.com 2023-02-15 07:36 出处:网络
Is there any easy way to move the records up and down using PHP开发者_运维知识库. for example if the records are in this order (A,B,C,D), when the user clicks the up button for record \'C\', then the

Is there any easy way to move the records up and down using PHP开发者_运维知识库. for example if the records are in this order (A,B,C,D), when the user clicks the up button for record 'C', then the display order has to change to (A,C,B,D). Is this efficient:

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

Is there any better ways to do this.


you can always just swap the values of the eleents you want to swap... list($b,$c)=array($c,$b);

<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 side:

$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]);
}


You tagged your question "mysql". Take a look at the ORDER BY sql statement. You can put an additional row called "priority" or similar into your table. Each entry gets a priority in ascending or descending order. When the user changes the order, you have to update two rows.


See sort(): http://it.php.net/manual/en/function.sort.php

0

精彩评论

暂无评论...
验证码 换一张
取 消