xtreme109
asked on
Manually reoder a php array
I have a page where users select check boxes of data and then sent to a confirmation page.
On the confirmation page the data is presented to them and as each item is displayed there are UP and DOWN arrows to reorder their selections.
What is the best way to accomplish this with php?
here is what i have so far, but it doesnt work please help!
I attached code below
I should mention that the data is caputured on the confirmation page using $_GET
On the confirmation page the data is presented to them and as each item is displayed there are UP and DOWN arrows to reorder their selections.
What is the best way to accomplish this with php?
here is what i have so far, but it doesnt work please help!
I attached code below
I should mention that the data is caputured on the confirmation page using $_GET
/*
$selectedBets looks like this:
array(3) {
[0]=>
string(5) "XXX_5"
[1]=>
string(5) "XXX_7"
[2]=>
string(5) "XXX_6"
}
************************
$itemToMove is sent through a querystring looking like
currentArrayIndex_UP|DOWN
for example: if i wanted to move index 0 to index 1
$itemToMove = 0_DOWN
if i wanted to move index 2 to index 1
$itemToMove = 2_UP
*/
function array_Reorder($selectedBets, $itemToMove){
$tempArr = explode("_",$itemToMove);
$oldKey = $tempArr[0];
$action = strtoupper($tempArr[1]);
$newKey = null;
$tmpNewArr = array();
echo "<br>action:$action<Br>";
echo "old key:$oldKey<Br>";
$tmpNewKey = $oldKey;
if($action == "UP"){
$tmpNewKey = $tmpNewKey - 1;
$tmpNewArr[$tmpNewKey] = $selectedBets[$oldKey];
}else if ($action == "DOWN"){
$tmpNewKey = $tmpNewKey + 1;
$tmpNewArr[$tmpNewKey] = $selectedBets[$oldKey];
}else{
return $selectedBets;
}
echo "new key:$tmpNewKey<Br><br>";
foreach($selectedBets as $key => $bets){
echo "$key == $tmpNewKey<br>";
$xKey = $key;
if($key == $tmpNewKey){
//skip this entry..we added it above manually
if($action == "UP"){
//echo $key+1 . "up<br>";
$xKey = $xKey + 1;
$tmpNewArr[$xKey] = $bets;
echo ($xKey). "--" . $bets . "<br>";
}else if ($action == "DOWN"){
$xKey = $xKey -1;
$tmpNewArr[$xKey] = $bets;
echo ($xKey) . "--" . $bets . "<br>";
}
}else{
//$tmpNewArr[$key] = $bets;
}
if($key == (count($selectedBets)-1)){break;}
}
//ksort($tmpNewArr);
echo "<pre>"; var_dump($tmpNewArr);echo "</pre>";
return $tmpNewArr;
}
You might find something in jQuery that will do this. Or you might just let the client place a numeric order into the original form showing which bets come in what order.
You are doing it in the difficult way...
First sort the items directly in the client with javascript and after they are sorted, save the final positions with php...
Doing it in his way is not only more efficient, is also more visual appealing...
So all that you need is:
http://jqueryui.com/demos/sortable/
http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/
Just edit the second example to submit the data to your php file instead of storing it in a cookie, and you are done...
Kind regards,
Oxyge+ Team
First sort the items directly in the client with javascript and after they are sorted, save the final positions with php...
Doing it in his way is not only more efficient, is also more visual appealing...
So all that you need is:
http://jqueryui.com/demos/sortable/
http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/
Just edit the second example to submit the data to your php file instead of storing it in a cookie, and you are done...
Kind regards,
Oxyge+ Team
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.