Oxymen Posted June 2, 2006 Share Posted June 2, 2006 Hi.I am making a page where I can edit my links.I have created two buttons which when clicked moves the link up or down the listby changing a ranking number in the mysql database.At first, my code seemed to work just fine untill i added a few more links and just moved them up and down at random to check that everything works as it should. at some point some links started to get the same ranking number, which messed up the whole list.The code is:[code]if (isset($_POST['group_up'])){if ($_POST['upvalue']==1){header( 'location:menu.php' );}else{$old = $_POST['upvalue'];$up = $_POST['upvalue']-1;$up_id = $_POST['group_id'];$conn = mysql_connect("$db_host","$db_user","$db_pass");if (!$conn) die ("Could not connect MySQL");mysql_select_db($db_database,$conn) or die ("Could not open database");$query = "UPDATE menu_groups SET ranking = $old WHERE ranking = $up;";mysql_query($query) or die('Error, query failed');$query = "UPDATE menu_groups SET ranking = $up WHERE group_id = $up_id;";mysql_query($query) or die('Error, query failed');}}[/code]And similar for the down button just it adds to the upvalue istead of subtract.Annyone notice why this code bugs?Also if you have suggestions on how to do this another way please tell me.Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/11047-php-to-change-link-ranking-in-mysql-database/ Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 What is $old for?Also, you should have an INDEX key, so the queries won't mess with the wrong row.Your last query seems to be correct though; but I am not fully understanding what you want and how the database is structured. Quote Link to comment https://forums.phpfreaks.com/topic/11047-php-to-change-link-ranking-in-mysql-database/#findComment-41271 Share on other sites More sharing options...
Oxymen Posted June 2, 2006 Author Share Posted June 2, 2006 my table have these columns:id, title and rankingNo two rows should have the same ranking number.The ranking number is for the ordering when I shal output the results.a low ranking means high placement in the output list.so if my table conainsid | title | ranking1 | Group1 | 22 | Group2 | 1group2 with id 2 will come first.What I want to do is to be able to change these ranking numbers in my admin page. this means that if I want to move an entry up the list, the ranking is lowered by one for this item. This also means that I have to make the entry with the previuos ranking the moved entry got, obtain the old ranking of the moved entry to ensure that no rows contains the same ranking number.I hope that made sense.. Quote Link to comment https://forums.phpfreaks.com/topic/11047-php-to-change-link-ranking-in-mysql-database/#findComment-41286 Share on other sites More sharing options...
Oxymen Posted June 3, 2006 Author Share Posted June 3, 2006 *bumb* Quote Link to comment https://forums.phpfreaks.com/topic/11047-php-to-change-link-ranking-in-mysql-database/#findComment-41577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.