Jump to content

PHP Sort Recordset by Array of ID's?


barkster

Recommended Posts

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

 

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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; 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.