Solved

Manually reoder a php array

Posted on 2011-02-18
3
226 Views
Last Modified: 2012-06-01
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

/*
$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;
    
}

Open in new window

0
Comment
Question by:xtreme109
3 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34926196
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.
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 34926209
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

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 125 total points
ID: 34926513
Or if you want this function to work I guess this would do:

<?php

//$selectedBets looks like this:
$selectedBets = array("XXX_5","XXX_7","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>"; 
    
    $tmpNewArr = $selectedBets;
    
    $val = $tmpNewArr[$tmpNewKey];    
    //echo"$val";
    
    foreach($tmpNewArr as $key => $bets){
         
        //echo "$key -> $bets<br>";

        //$xKey = $key;
        if($key == $tmpNewKey){

            $tmpNewArr[$tmpNewKey] = $tmpNewArr[$oldKey];
            $tmpNewArr[$oldKey] = $bets;
            break; 

         }else{
         $tmpNewArr[$key] = $bets;
             
         }
         //if($key == (count($selectedBets)-1)){break;}
                             print_r($tmpNewArr);echo"<br/><br/>";
    }
    
        //ksort($tmpNewArr);
        //echo "<pre>"; print_r($tmpNewArr);echo "</pre>";
        return $tmpNewArr;
    
}

print_r($selectedBets);
$selectedBets = array_Reorder($selectedBets, "1_UP");
print_r($selectedBets);
?>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
forgetful about js objects 8 46
Binding a Grid in Javascript 5 42
Restricting the scope of variables to private in OOP 4 21
Autocomplete with Jquery Question 2 19
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question