Jordi
01-29-2005, 07:35 AM
Hello everybody,
I am currently building something kind of like a forum for a project and I want the administrator to be able to move forums up and down to change the order that they appear on the screen. For this I have written the following function:
function changeOrder ($forum, $order, $action) {
if ($forum) {
$where = "AND forum_id='$forum'";
$table = "topics";
$order_type = "topic_order";
}
else {
$table = "forums";
$order_type = "forum_order";
}
if ($action == "decrease") $next = $order-1;
else $next = $order+1;
$sql1 = "UPDATE $table SET $order_type=-1 WHERE $order_type=$next $where";
$sql2 = "UPDATE $table SET $order_type=$next WHERE $order_type=$order $where";
$sql3 = "UPDATE $table SET $order_type=$order WHERE $order_type=-1 $where";
$sql4 = "SELECT name, $order_type FROM $table WHERE $order_type=$next $where";
$sql5 = "SELECT name, $order_type FROM $table WHERE $order_type=-1 $where";
$sql6 = "SELECT name, $order_type FROM $table WHERE $order_type=$order $where";
$sql7 = "SELECT name, $order_type FROM $table WHERE $order_type=$next $where";
$sql8 = "SELECT name, $order_type FROM $table WHERE $order_type=-1 $where";
$sql9 = "SELECT name, $order_type FROM $table WHERE $order_type=$order $where";
$test = mysql_fetch_object(mysql_query($sql4)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql1) or die (mysql_error()); echo $sql1."<br />";
$test = mysql_fetch_object(mysql_query($sql5)); echo $test->name.", ".$test->$order_type."<br />";
$test = mysql_fetch_object(mysql_query($sql6)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql2) or die (mysql_error()); echo $sql2."<br />";
$test = mysql_fetch_object(mysql_query($sql7)); echo $test->name.", ".$test->$order_type."<br />";
$test = mysql_fetch_object(mysql_query($sql8)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql3) or die (mysql_error()); echo $sql3."<br />";
$test = mysql_fetch_object(mysql_query($sql9)); echo $test->name.", ".$test->$order_type."<br />";
}
A lot of these rules are of course not necessary, but I put them there because the function doesn't work and I wanted to print out why.
This is an example of what I would get if I would call on this function:
forum2, 2 (forumname followed by forum_order)
UPDATE jbieger_debat_forums SET forum_order=-1 WHERE forum_order=2
forum2, -1
forum3, 3
UPDATE jbieger_debat_forums SET forum_order=2 WHERE forum_order=3
forum3, 2
forum2, -1
UPDATE jbieger_debat_forums SET forum_order=3 WHERE forum_order=-1
forum2, 3
This is exactly how it should work. The only problem is that my MySQL-table doesn't get updated. At least, something like that is happening. This is very strange though, because with $sql4-9 I am getting information out of the (updated) table and this information seems to be correct! And indeed, on the forum-page, the forums would be switched.
However when I come back to that page, I find that the forums have automatically switched back. Also, when I would look in phpMyAdmin (even immideately after I called the function), nothing appears to have changed.
Some more oddities:
If I would copy the update-queries into the SQL-form of phpMyAdmin, everything works perfectly and the table gets updated.
Also, if I would call the three update-queries seperately, they do seem to work. I would do that like this:
mysql_query($sql1);
//mysql_query($sql2);
//mysql_query($sql3);
reload page
//mysql_query($sql1);
mysql_query($sql2);
//mysql_query($sql3);
reload page
//mysql_query($sql1);
//mysql_query($sql2);
mysql_query($sql3);
This does work.
Does anyone have any idea about what might be going wrong and what I can do about it?
Thanks.
I am currently building something kind of like a forum for a project and I want the administrator to be able to move forums up and down to change the order that they appear on the screen. For this I have written the following function:
function changeOrder ($forum, $order, $action) {
if ($forum) {
$where = "AND forum_id='$forum'";
$table = "topics";
$order_type = "topic_order";
}
else {
$table = "forums";
$order_type = "forum_order";
}
if ($action == "decrease") $next = $order-1;
else $next = $order+1;
$sql1 = "UPDATE $table SET $order_type=-1 WHERE $order_type=$next $where";
$sql2 = "UPDATE $table SET $order_type=$next WHERE $order_type=$order $where";
$sql3 = "UPDATE $table SET $order_type=$order WHERE $order_type=-1 $where";
$sql4 = "SELECT name, $order_type FROM $table WHERE $order_type=$next $where";
$sql5 = "SELECT name, $order_type FROM $table WHERE $order_type=-1 $where";
$sql6 = "SELECT name, $order_type FROM $table WHERE $order_type=$order $where";
$sql7 = "SELECT name, $order_type FROM $table WHERE $order_type=$next $where";
$sql8 = "SELECT name, $order_type FROM $table WHERE $order_type=-1 $where";
$sql9 = "SELECT name, $order_type FROM $table WHERE $order_type=$order $where";
$test = mysql_fetch_object(mysql_query($sql4)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql1) or die (mysql_error()); echo $sql1."<br />";
$test = mysql_fetch_object(mysql_query($sql5)); echo $test->name.", ".$test->$order_type."<br />";
$test = mysql_fetch_object(mysql_query($sql6)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql2) or die (mysql_error()); echo $sql2."<br />";
$test = mysql_fetch_object(mysql_query($sql7)); echo $test->name.", ".$test->$order_type."<br />";
$test = mysql_fetch_object(mysql_query($sql8)); echo $test->name.", ".$test->$order_type."<br />";
mysql_query($sql3) or die (mysql_error()); echo $sql3."<br />";
$test = mysql_fetch_object(mysql_query($sql9)); echo $test->name.", ".$test->$order_type."<br />";
}
A lot of these rules are of course not necessary, but I put them there because the function doesn't work and I wanted to print out why.
This is an example of what I would get if I would call on this function:
forum2, 2 (forumname followed by forum_order)
UPDATE jbieger_debat_forums SET forum_order=-1 WHERE forum_order=2
forum2, -1
forum3, 3
UPDATE jbieger_debat_forums SET forum_order=2 WHERE forum_order=3
forum3, 2
forum2, -1
UPDATE jbieger_debat_forums SET forum_order=3 WHERE forum_order=-1
forum2, 3
This is exactly how it should work. The only problem is that my MySQL-table doesn't get updated. At least, something like that is happening. This is very strange though, because with $sql4-9 I am getting information out of the (updated) table and this information seems to be correct! And indeed, on the forum-page, the forums would be switched.
However when I come back to that page, I find that the forums have automatically switched back. Also, when I would look in phpMyAdmin (even immideately after I called the function), nothing appears to have changed.
Some more oddities:
If I would copy the update-queries into the SQL-form of phpMyAdmin, everything works perfectly and the table gets updated.
Also, if I would call the three update-queries seperately, they do seem to work. I would do that like this:
mysql_query($sql1);
//mysql_query($sql2);
//mysql_query($sql3);
reload page
//mysql_query($sql1);
mysql_query($sql2);
//mysql_query($sql3);
reload page
//mysql_query($sql1);
//mysql_query($sql2);
mysql_query($sql3);
This does work.
Does anyone have any idea about what might be going wrong and what I can do about it?
Thanks.