winter_kiter Posted August 21, 2006 Share Posted August 21, 2006 I have a table, with an primary key ID and i want to change the order of these keys something like this: 1->12->23->44->35->5in the end the user should be able to move a row(an entry) up and down, any ideas?Thx flavio Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Share Posted August 21, 2006 Do you want to do something Like this???Its done in PHP, just click on the first link.Not sure if that is the sort of thing you are looking for. Link to comment Share on other sites More sharing options...
winter_kiter Posted August 21, 2006 Author Share Posted August 21, 2006 exactly can i have the code? Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Share Posted August 21, 2006 Heres the code for the form and the dropdown lists. Took me a while to figure out how to set up the loops. if ($_REQUEST['action'] == "todo") { echo "My <b><font color=\"red\">School</b></font> To Do List"; echo "<br><br>"; if ($_REQUEST['subaction'] == "edititem") { $query="select * from list_items where priority >= '1' and l_id='$_REQUEST[l_id]' order by priority"; $results=mysql_query($query); echo mysql_error(); $e=mysql_fetch_array($results)?> <form name="addtodo" method="post" action="todo.php?action=edittodo&l_id=<?=$e[l_id]?>&groups=<?=$e[groups]?>"> Item: <input type="text" name="item" size="10" value="<?=$e[item]?>"> Link: <input type="text" name="links" size="10" value="<?=$e[links]?>"> <input type="submit" value="Update!"><br> </form><? } else { } $query="select * from list_items where priority >= '1' and groups='school' order by priority"; $results=mysql_query($query); echo mysql_error(); while ($e=mysql_fetch_array($results)) { $new=1; $rows = mysql_num_rows($results); echo mysql_error(); echo "<select name=\"p\" onChange=\"MM_jumpMenu('parent',this,0)\">"; while ($new <= $rows) { echo "<option value=\"todo.php?action=changepriority&oldpriority=".$e[priority]."&newpriority=".$new."&l_id=".$e[l_id]."&groups=".$e[groups]."\""; if ($e[priority] == $new ) { echo " selected>".$new."</option><br>"; } else { echo ">".$new."</option><br>"; } $new++; } if ($e[links] == "") { echo "</select><a href=\"todo.php?action=deleteitem&l_id=$e[l_id]&delpriority='$e[priority]'&groups=$e[groups]\"><img src=\"delete.gif\" border=\"0\" height=\"20\" width=\"20\"></a><a href=\"todo.php?action=schooltodo&subaction=edititem&l_id=$e[l_id]\"><img src=\"edit.gif\" border=\"0\" height=\"20\" width=\"20\"></a>".$e[item]."<br><br>"; } else { echo "</select><a href=\"todo.php?action=deleteitem&delpriority=$e[priority]&l_id=$e[l_id]&groups=$e[groups]\"><img src=\"delete.gif\" border=\"0\" height=\"20\" width=\"20\"></a><a href=\"todo.php?action=schooltodo&subaction=edititem&l_id=$e[l_id]\"><img src=\"edit.gif\" border=\"0\" height=\"20\" width=\"20\"></a><a href=\"".$e[links]."\">".$e[item]."</a><br><br>"; } } ?> <form name="addtodo" method="post" action="todo.php?action=addtodo"> Item: <input type="text" name="item" size="10"> Link: <input type="text" name="links" size="10"> <input type="submit" value="add!"><br> <input type="radio" name="groups" value="school">School<input type="radio" name="groups" value="work">Work<input type="radio" name="groups" value="finance">Financial<input type="radio" name="groups" value="other">Other </form> <? } And heres the code to interact with the database and change the priority. if ($_REQUEST['action'] == "changepriority"){ // This is what happens when the old priority is Less than the new priority =================================================================== if ($_REQUEST[oldpriority] < $_REQUEST[newpriority]) { $newpriority=$_REQUEST[newpriority]; $oldpriority=$_REQUEST[oldprioirty]; $change=$newpriority - $oldpriority; $other=$change+$oldpriority+1; $query="update list_items set priority = priority - 1 where priority > '$_REQUEST[oldpriority]' and priority < '$other' and groups='$_REQUEST[groups]'";mysql_query($query); $query="update list_items set priority='$newpriority' where l_id='$_REQUEST[l_id]' and groups='$_REQUEST[groups]'"; mysql_query($query); } //========================================================================================================================================== //This is what happens when the old priority is greater than the new priority ========================== elseif ($_REQUEST[oldpriority] > $_REQUEST[newpriority]) { $query="update list_items set priority=priority+1 where priority >= '$_REQUEST[newpriority]' and priority <= '$_REQUEST[oldpriority]' and groups='$_REQUEST[groups]'";mysql_query($query); $query="update list_items set priority='$_REQUEST[newpriority]' where l_id='$_REQUEST[l_id]' and groups='$_REQUEST[groups]'"; mysql_query($query); } //======================================================================================================} And heres the SQL so you can make the table in your database. So its a little easier to see how its setup. CREATE TABLE `list_items` ( `l_id` int(11) NOT NULL auto_increment, `priority` int(11) NOT NULL, `item` varchar(32) NOT NULL, `links` varchar(64) NOT NULL, `groups` varchar(16) NOT NULL, PRIMARY KEY (`l_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21; Link to comment Share on other sites More sharing options...
winter_kiter Posted August 21, 2006 Author Share Posted August 21, 2006 thanks Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Share Posted August 21, 2006 Good luck, hope you get it working. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now