Author Topic: PHP Sort Recordset by Array of ID's?  (Read 1462 times)

0 Members and 1 Guest are viewing this topic.

Offline barksterTopic starter

  • Enthusiast
    • View Profile
PHP Sort Recordset by Array of ID's?
« on: October 02, 2008, 09:42:28 PM »
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

Offline thorpe

  • Global Moderator
  • 'Mind Boggling!'
  • *
    • View Profile
Re: PHP Sort Recordset by Array of ID's?
« Reply #1 on: October 02, 2008, 10:34:03 PM »
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...

Code: [Select]
<?php

  
foreach ($array as $k => $v) {
    
$sql "UPDATE tbl SET sortorder = $v WHERE id = $k";
    
mysql_query($sql);
  }

?>

Offline barksterTopic starter

  • Enthusiast
    • View Profile
Re: PHP Sort Recordset by Array of ID's?
« Reply #2 on: October 03, 2008, 08:15:59 AM »
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?

Offline barksterTopic starter

  • Enthusiast
    • View Profile
Re: PHP Sort Recordset by Array of ID's?
« Reply #3 on: October 03, 2008, 09:23:30 AM »
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?

Code: [Select]
$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;
}


PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.