Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-01-2009, 07:26 PM Save order of items
Extreme Talker

Posts: 176
Trades: 0
This is kinda more of logic question, because I already know how to code it, but it's coded in PHP/MySQL so I thought I'd post here.


I have a site, where users can register/login.
The user gets their own homepage where they can add different sections and links inside each section.

My problem is what to do with the order of the links. I want users to be able to customize the order.

What I've done so far is:

Each link has a 'place', which is just a number.

When you add a link, it get's the place of the last link in that section, and adds one, so as to put it in last place.

When you delete a link, it removes that link, then subtracts 1 from every links 'place' that come after it.


Now I want to make it so users can change the place. And I don't really know how I should go about it.

I guess you'd pick a new place for it, and some how renumber all the ones after it.

It's really confusing, is there any easier ways? Or how about examples


This is the current code when something get's deleted:
PHP Code:
            $placesql mysql_query("SELECT * FROM sections WHERE column_id = '$deletedcolumn' AND place > '$deletedplace'");
            
            while (
$needsupdate mysql_fetch_array($placesql)) {
             
            
$newpid $needsupdate[id];
            
$newp $needsupdate[place] - 1;
            
            
mysql_query("UPDATE sections SET place = '$newp' WHERE id = '$newpid'");
            } 
I'm assuming the move code will have to be longer.



If anyone has any thoughts on this subject, I'd appreciate it.

Sorry for a rather messy post.

Last edited by Skeddles; 04-01-2009 at 07:38 PM..
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-01-2009, 08:00 PM Re: Save order of items
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
One way would be to allow the user to assign priorities to each link. A link with a higher priority is displayed first. Links with equal priority are displayed in whatever order they are retrieived from the database. This lets you just select the links and sort by priority, without actually worring about assigning unique positions to each link.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-01-2009, 09:36 PM Re: Save order of items
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
You can also use something like this:

PHP Code:
<?php
// $new_position is defined to be the new place number for the column_id
// $column_to_move is defined to be the column you wish to move.

mysql_query("UPDATE sections SET place = place + 1` WHERE place >= '".$new_position."'");
mysql_query("UPDATE sections SET place = ".$new_position."` WHERE column_id=".$column_to_move);
?>
Now, I've only taken a guess at the names based off of what you provided since you didn't include the table structure for your sections table, but it should give you the idea.

You'll note also that it is not necessary to loop through each result and update as I've shown in the code above, so you can modify your current routines.

One thing of concern is that your queries seem to have nothing filtered by a specific user or account, so these changes will apply to all entries within the sections table -- not likely something you'd intended.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Save order of items
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21091 seconds with 12 queries