I have a form and on this form the user can change the display order. This is reflected on a page that shows all our employees and we want to be able to control which order they appear.
When the form is submitted, I want to alter the other display orders in the database to reflect the change.
Some example data:
When they save that form, everything is updated to the db except the display order. That is handled in a separate function call.
function updateDisplayOrder($eid, $display_order_old, $display_order)
{
if ($eid > 0)
{
if ($display_order != $display_order_old)
{
$result = db_query("SELECT * FROM {help_employees} WHERE active = 1 ORDER BY display_order");
while ($arr = db_fetch_array($result))
{
if ($display_order > $display_order_old)
{
if ($arr["display_order"] > $display_order_old && $arr["display_order"] <= $display_order)
{
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", array($arr["display_order"] - 1, $arr["eid"]));
}
}
else
{
if ($arr["display_order"] < $display_order_old && $arr["display_order"] >= $display_order)
{
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", array($arr["display_order"] + 1, $arr["eid"]));
}
}
}
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", $display_order, $eid);
}
}
}
What happens now: