I’m not sure how to do this or even if it’s possible. I have a table of entries, but I’d like to change the running order of the entries. I’ve got a field called ‘ID’ which is set to auto-increment. I want the people in the back end to change the order of the entries themselves. So for example, if they move one entry up then the one above will automatically move down.
I’ve put up and down arrows by each entry, but am not sure what to do next. The code I’ve written is:
<?php
$id = $_GET['id'];
$direction = $_GET['direction'];
$menu = $_GET['menu'];
con = mysql_connect("hostname", "user", "password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("table", $con);
$sql = "SELECT priority FROM Menu WHERE ID = $_GET[id]";
list($curpos) = mysql_fetch_array(mysql_query($sql));
// Find new position
if ($direction > 0)
{
// To be moved up
$sql = "SELECT priority FROM Menu WHERE priority < $curpos AND ID = $id ORDER BY priority DESC LIMIT 1";
list($newpos) = @mysql_fetch_array(mysql_query($sql));
}
else
{
// To be moved down
$sql = "SELECT priority FROM Menu WHERE priority > $curpos AND ID = $id ORDER BY priority ASC LIMIT 1";
list($newpos) = @mysql_fetch_array(mysql_query($sql));
}
if ($newpos)
{
$sql = "UPDATE Menu SET priority = $curpos WHERE priority = $newpos AND Menu = $menu";
mysql_query($sql);
$sql = "UPDATE Menu SET priority = $newpos WHERE ID = $id";
mysql_query($sql);
}
echo $msg; ?>
but this doesn’t seem to do anything, I’ve seen it working in things like cPanel’s but really don’t know how to implement it myself.
I’d be grateful for any help.
Do not touch the ids for that. Create a new column “sorting” and use that. Changing the ID might cause problems and is not nice