Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/156192-solved-recursive-1-on-position-field/
Share on other sites

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.

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;
	}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.