More CMS help

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm building a CMS, and have run into another problem that I'm not sure  
which direction to take on it. Basically, I allow users to add pages, and  
allow them to hide/unhide links (sub-navigation) on any page they wish. The  
problem now is in the link order. I want them to be able to choose the link  
order, and am not sure of the best way to handle this.

I have considered up and down arrows, however I'm not really sure of the  
code I'll need to have them query the DB based on that (would I simply  
subtract or add one to the linkOrder to the DB?). So, we have also discussed  
the use of a drop-down list with a list of numbers showing the current order  
(from the DB) that each link is in, along with numbers to change the order  
by clicking a down arrow and submitting a form.

My question is mainly - which method would be best? What would the code, or  
even the algorithm look like here? The select box code is looking ugly as  
hell because I want to echo all of this while I'm echoing the actual links,  
and the up/down arrow choices seems confusing as far as the algorithm to me.  
Here's the code block I'm using to echo values from the DB as links on the  

    //start a table
    echo "<table width=\"150\"><tr><td></td></tr>";

    //call the getLinks function (from a displayClass I have built)
    $links = $displayData->getLinks($pageID, $contentID, $data);

    //start looping through the records

        //echo links in table cells along with a hide link to have a link  
NOT show up on the actual site
        echo "<tr><td><a  


echo "</table>";


Thanks for any help  

Re: More CMS help

This is how I normally achieve a sortable list:

You should start by setting up a SortOrder (int) column.  Each new
record that get's added should cause that # to increment.  If this is
going to be a multi-user system or something you should increment it
based on that UserID (so there aren't gaps in the number).  Then when
you want to move up simply do:

function MoveUp($id,$replaceID)
UPDATE table SET SortOrder=(SortOrder-1) WHERE ID=$id
UPDATE table SET SortOrder=(SortOrder+1) WHERE ID=$replaceID

move down would be similar

function MoveDown($id,$replaceID)
UPDATE table SET SortOrder=(SortOrder+1) WHERE ID=$id
UPDATE table SET SortOrder=(SortOrder-1) WHERE ID=$replaceID

And of course on your select you would ORDER BY SortOrder.  There are
several ways you can accomplish this task, this is just the way I do
it.  I don't know if this is necessarily the best way, but as long as
your script doesn't leave gaps in the SortOrder # you'll be fine.

Re: More CMS help

Ok, I've added a linkOrder ID into the DB, and have my up/down arrows  
passing order=up and order=down now - My problem now is not incrementing or  
decrementing the current id chosen, it's finding the OTHER IDs that will be  
affected. Do I literally decrement every orderID for the current pageID? If  
the pageIDs are scattered, I really will not know at all what the current ID  
is save what came from the DB.

Quoted text here. Click to load it

Re: More CMS help

Does this fix the problem?  It will attempt to find the "replaceID"
automatically.  You shouldn't need to shift every item, just the item
before or after the item you want to move.

function MoveUp($id)
$query = "SELECT ID FROM table WHERE SortOrder=(SortOrder+1)";
$ret = mysql_query($query);
if (mysql_num_rows($ret) == 1)
// if 0 rows returned, don't change the sortorder
list($replaceID) = mysql_fetch_row($ret);

$query = "UPDATE table SET SortOrder=(SortOrder-1) WHERE ID=$id"

$query = "UPDATE table SET SortOrder=(SortOrder+1) WHERE ID=$replaceID"

Re: More CMS help

I don't understand the query here - If I'm selecting WHERE  
SortOrder=(SortOrder+1) - I don't know what sortOrder is. Do I have to pass  
the current sortOrder ID within the query string?

Quoted text here. Click to load it

Re: More CMS help

oops, you're absolutely right, I don't know what I was thinking.  How
about something like this

$query = "SELECT ID FROM table WHERE SortOrder=((SELECT SortOrder FROM
table WHERE ID=$id) + 1)";

I don't have any of my PHP or MySQL in front of me, so I'm not testing
this stuff.

Site Timeline