barkster Posted October 3, 2008 Share Posted October 3, 2008 I'm trying to build a script to dynamically create div's and make them draggable base on a sample like this http://www.stillnetstudios.com/2006/12/31/drag-and-drop-sort-order-with-scriptaculous/ After the user resorts the div's I need to store that sort order in the database and then when page reloads I need to somehow to resort them when the page is reloaded. I was thinking of storing the sort order into an array based on the div's recordID. Then that is where I'm lost. Do insert the records into an array at load and them sort them somehow based on the sort array to reload them back into the correct order or is there some easier way? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted October 3, 2008 Share Posted October 3, 2008 You would need a special field in your table to sort by. sortorder. Then all you would need is an array containing each record's id as the key and the sortorder as value. Then... <?php foreach ($array as $k => $v) { $sql = "UPDATE tbl SET sortorder = $v WHERE id = $k"; mysql_query($sql); } ?> Quote Link to comment Share on other sites More sharing options...
barkster Posted October 3, 2008 Author Share Posted October 3, 2008 Yeah was looking for a way to do it without having to update every row every time they changed the order. I was think of something like this but don't know how to implement Table->task taskid,name,description Table->sortorder sortid,order order would be an array based on the taskids so something like "5,1,2,4,3" and then only update the sortorder table when they make a change since it will only be needed the next time the page is refreshed. Then when page is loaded do something like loading the tasks table into an array and somehow sorting/displaying it based on the order array from the sortorder table? Quote Link to comment Share on other sites More sharing options...
barkster Posted October 3, 2008 Author Share Posted October 3, 2008 Found this website http://www.the-art-of-web.com/php/sortarray/ looks like what I want to do in section 6. Sorting based on a list of values but I have no idea what $b is and how to use it? $data = array( array("name" => "Mary Johnson", "position" => "Secretary"), array("name" => "Amanda Miller", "position" => "Member"), array("name" => "James Brown", "position" => "Member"), array("name" => "Patricia Williams", "position" => "Member"), array("name" => "Michael Davis", "position" => "President"), array("name" => "Sarah Miller", "position" => "Vice-President"), array("name" => "Patrick Miller", "position" => "Member") ); $sortorder = array( 'President', 'Vice-President', 'Secretary', 'Member' ); function mySort($a, $b) { global $sortorder; if($a['position'] == $b['position']) { return 0; } $cmpa = array_search($a['position'], $sortorder); $cmpb = array_search($b['position'], $sortorder); return ($cmpa > $cmpb) ? 1 : -1; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.