Return to Snippet

Revision: 10089
at December 7, 2008 22:08 by Zync


Updated Code
function changeOrderNum($currentOrderNum, $newOrderNum) {
	$yourTableName = "";
	$orderField = "";

	if($newOrderNum < $currentOrderNum) {
		$update1 = mysql_query("UPDATE $yourTableName SET $orderField = $orderField+1 WHERE $orderField >= '$newOrderNum' AND $orderField < '$currentOrderNum'") or die(mysql_error());
	} else {
		$update1 = mysql_query("UPDATE $yourTableName SET $orderField = $orderField-1 WHERE $orderField <= '$newOrderNum' AND $orderField > '$currentOrderNum'") or die(mysql_error());
	}
}

Revision: 10088
at December 7, 2008 22:07 by Zync


Initial Code
function changeOrderNum($currentOrderNum, $newOrderNum) {
	$yourTableName = "";
	$orderField = "";

	if($newOrderNum < $currentOrderNum) {
		update1 = mysql_query("UPDATE $yourTableName SET $orderField = $orderField+1 WHERE $orderField >= '$newOrderNum' AND $orderField < '$currentOrderNum'") or die(mysql_error());
	} else {
		$update1 = mysql_query("UPDATE $yourTableName SET $orderField = $orderField-1 WHERE $orderField <= '$newOrderNum' AND $orderField > '$currentOrderNum'") or die(mysql_error());
	}
}

Initial URL

                                

Initial Description
Quick little snippet for changing the ordering of a field in a MYSQL database.

Initial Title
Change ordering position of MYSQL field with PHP

Initial Tags
mysql, php, order

Initial Language
PHP