rondog Posted April 29, 2009 Share Posted April 29, 2009 I have a list of items that a user can order by clicking 'move up' / 'move down'. These items are in my DB and when SELECTing I ORDER BY position. 'Position' is a field name that just go 1 through X. The user also has the ability to delete an item by them clicking 'delete' on the specific item. Say a user deletes Item 5 out of 10. I will now have a gap in my position field - [1,2,3,4,6,7,8,9,10] How can I ensure that when the users delete's an item, all items after it move their position number down 1 as well? Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/ Share on other sites More sharing options...
Daniel0 Posted April 29, 2009 Share Posted April 29, 2009 Select them and recalculate their position values according to their order. SELECT id FROM foo WHERE bar='something' ORDER BY position ASC; Now loop through them. Start with position=1 and increment it on each iteration. Then save to database. Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822272 Share on other sites More sharing options...
Mark Baker Posted April 29, 2009 Share Posted April 29, 2009 UPDATE table SET position=position-1 WHERE id > $deletedItemID Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822281 Share on other sites More sharing options...
Daniel0 Posted April 29, 2009 Share Posted April 29, 2009 Heh... that'll work too Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822283 Share on other sites More sharing options...
rondog Posted April 29, 2009 Author Share Posted April 29, 2009 thanks guys. I'll give that a try Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822286 Share on other sites More sharing options...
rondog Posted April 29, 2009 Author Share Posted April 29, 2009 Is there a problem with my query? Its returning false and just deleting the row: <?php function deleteProjectItem($id,$projID) { $query = mysql_query("DELETE FROM projectData WHERE id = '$id' AND proj_id = '$projID'"); $renum = mysql_query("UPDATE projectData SET position = 'position-1' WHERE id > '$id' AND projID = '$projID'"); if ($renum) { return true; } else { return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822306 Share on other sites More sharing options...
Daniel0 Posted April 29, 2009 Share Posted April 29, 2009 'position-1' is a string literal whereas position-1 is the field name called position subtracted by 1. Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822308 Share on other sites More sharing options...
rondog Posted April 29, 2009 Author Share Posted April 29, 2009 Ahh ok gotcha..I had that before and it was still giving me false..I guess it would help to name my field correctly. I had: AND projID = '$projID' should have been: AND proj_id = '$projID' It is working now thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/#findComment-822310 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.