Friday, July 30, 2010
 

PHP sorting algorithms

One day looking for a sorting algorithm spent some time and did not find anything good. Actually found, but it needed some adoption. So decided paste it here, maybe someone it will save some time:

I give you free main functions.

  • First moves item up.
  • Second moves item down.
  • Third one moves item down or up by number of positions depending on given parameter.
function moveUp($ItemID){
global $conf;
$qResult = parent::selectArr(”SELECT * FROM {$this->guideName} WHERE id=’$ItemID’ ” );
$linkID = $qResult[1][’id’];
$placement = $qResult[1][’position’];
$qResult = parent::selectArr( “SELECT id, position FROM {$this->guideName}
WHERE position < ‘$placement’
ORDER BY position DESC” );
$newPlacement = $qResult[1][’position’];
$listid = $qResult[1][’id’];
if ( $newPlacement == $placement )
{
$placement += 1;
}
if ( is_numeric( $listid ) )
{
parent::query( “UPDATE {$this->guideName} SET position=’$newPlacement’ WHERE id=’$linkID’” );
parent::query( “UPDATE {$this->guideName} SET position=’$placement’ WHERE id=’$listid’” );
}
}

function moveDown($ItemID){
global $conf;
$qResult = parent::selectArr(”SELECT * FROM {$this->guideName} WHERE id=’$ItemID’ ” );
$linkID = $qResult[1][’id’];
$placement = $qResult[1][’position’];
$qResult = parent::selectArr( “SELECT id, position FROM {$this->guideName}
WHERE position > ‘$placement’
ORDER BY position ASC” );
$newPlacement = $qResult[1][’position’];
$listid = $qResult[1][’id’];
if ( $newPlacement == $placement )
{
$placement += 1;
}
if ( is_numeric( $listid ) )
{
parent::query( “UPDATE {$this->guideName} SET position=’$newPlacement’ WHERE id=’$linkID’” );
parent::query( “UPDATE {$this->guideName} SET position=’$placement’ WHERE id=’$listid’” );
}
}

function moveLarge($ItemID,$new_position){

$SQL = “SELECT position FROM {$this->guideName} WHERE id = {$ItemID}”;
$curPost = $this->selectSingle($SQL,’position’);

if ($curPost > $new_position)
{
for ($i = 1;$i <= ($curPost - $new_position); $i++ ) $this->moveUp($ItemID);
}
elseif ($curPost < $new_position)
{
for ($i = 1;$i <= ( $new_position - $curPost); $i++ ) $this->moveDown($ItemID);
}

}
Back »

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com