Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2010, 10:57 PM   PM User | #1
speck
New Coder

 
Join Date: Nov 2009
Posts: 78
Thanks: 7
Thanked 0 Times in 0 Posts
speck is an unknown quantity at this point
Question Navigation link sorting for custom cms

I am building a small cms where the user can add, edit, and removed pages. Each time the user adds or edits a page, they can specify the order placement of that page within the navigation.

I have a field called sort_order where the user can add the sort order number. What I am wanting is when the user adds a sort number that is already assigned to another page, that the page with that sort number is bumped down along with any other pages underneath.

I hope this makes sense. If at all possible, it would be cool to integrate this with a drag and drop function instead of having to manually type a number. Thanks in advance for the help!
speck is offline   Reply With Quote
Old 03-30-2010, 11:19 PM   PM User | #2
xconspirisist
Regular Coder

 
xconspirisist's Avatar
 
Join Date: Jun 2006
Location: Great Britain.
Posts: 137
Thanks: 1
Thanked 6 Times in 6 Posts
xconspirisist has a little shameless behaviour in the past
Pfft. I imagine something like this;

PHP Code:

$thingId 
13// The thing you are bumping, or moving, has an ID of 13.
$thingSortOrder 2// We are moving this item to position 2.

$sql 'SELECT id, sort_order FROM things WHERE sort_order = "' $thingSortOrder '" LIMIT 1';
$result $db->query($sql);

if (
$result->numRows() != 0) {
   
// Something else has this sort order, so lets bump everything with a HIGHER sort order by 1.
   
$sql 'UPDATE things SET sort_order = (sort_order + 1) WHERE sort_order >= "' $thingSortOrder '" '
   
$result $db->query($sql);
}

// Now lets use the sort order that we wanted.
$sql 'UPDATE things SET sort_order = "' $thingSortOrder '" WHERE id = "' $thingId '" LIMIT 1';
$result $db->query($sql); 
This will mean that you will eventually end up with sort orders which are not consecutive (eg: 1, 2, 7, 8, 9, 15, 16), but that should not really matter. This should be fine with a table of about 50 rows or so, with a maximum potential update of 49+1 rows, but this is an admin function and on average, those conditions will be pretty rare.
__________________
If I have been helpful, use the "thank" button - It makes me happy!

xconspirisist.co.uk - homepage of my online alias
technowax.net - a community for people interested in all forms of modern technology.

Last edited by xconspirisist; 03-30-2010 at 11:22 PM..
xconspirisist is offline   Reply With Quote
Old 03-30-2010, 11:21 PM   PM User | #3
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Add a time field as well and sort by position and time updated. Multiple items with the same position id will be sorted by time they were updated.
MattF is offline   Reply With Quote
Reply

Bookmarks

Tags
cms, navigation, sorting

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.