Need help using form to set order of pages on dynamic websit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mosk
Forum Newbie
Posts: 9
Joined: Mon Jun 08, 2009 6:49 am

Need help using form to set order of pages on dynamic websit

Post by mosk »

Hi. I went through a tutorial to build a dynamic website in order to facillitate my learning php. One of the steps was to create a simple form so staff members could add new sections for the site. The form includes a Position field, so staff can enter a numeric position for that section to show up in a slist (so a non techy staff member could easily add a Contact Us area and assign it as the second section just by typing Contact Us into the name field of the form and 2 into the position field.

The problem I ran into is that the form does NOT update the positions of other sections that already exist, so you may end up with Products and Contact Us both being assigned to the second position. I'm not sure if I need to use some if / then statements to reassign positions to sections that were already in the database or if there's an array function that will automatically 'slide' the existing sections over so if a new section is designated position 2, all the old sections that had been position 2 or higher will be changed to position n+1.

I've done some reading and worked through a bunch of tutorials, but php is still new to me, so any advice would be much appreciated.

I've pasted some of the code below:

Code: Select all

 
 
    $id = mysql_prep($_GET['page']);
        $menu_name = trim(mysql_prep($_POST['menu_name']));
        $position = mysql_prep($_POST['position']);
        $visible = mysql_prep($_POST['visible']);
        $content = mysql_prep($_POST['content']);
        
    //Database submission only proceeds if there were NO errors.
        
        if(empty($errors)){
        $query = "UPDATE pages SET
                menu_name = '{$menu_name}',
                position = {$position},
                visible = {$visible},
                content = '{$content}'
                WHERE id = {$id}";
                $result = mysql_query($query, $connection);
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Need help using form to set order of pages on dynamic websit

Post by mikemike »

How do you want to order the new elements if the ID's match? If it's by, say, alphabetical order, then you're trying to fix something that doesn't need solving. Simply order by both fields...

Code: Select all

SELECT * FROM TABLE ORDER BY sort_order,name
The above will order by the field 'sort_order' (which is your int sort), if both are identical it will order by 'name' (which is the name of the item, such as 'Contact Us').
mosk
Forum Newbie
Posts: 9
Joined: Mon Jun 08, 2009 6:49 am

Re: Need help using form to set order of pages on dynamic websit

Post by mosk »

I want to be able to sort by the numerical position entered on the form (by a person who will not know anything about php).

Let's say the database currently has the following:
Position 1 Welcome Section
Position 2 Our Products
Position 3 Pricing

The Staff user then uses a form to add Contact Us and wants it in Position 2.

As the code now stands, Our Products and Contact Us will both be assigned Position 2.
I'd like to code things so Our Products and Pricing would automatically get bumped to positions 3 and 4 when Contact us is added to position 2. If I set Position to be Unique from within PHP MyAdmin, it gets rid of the problem of duplicate entires for one position, BUT it does so by not letting the User choose a position that's already occupied.

Suggestions?

Thanks.
Post Reply