Welcome Guest, Not a member yet? Register   Sign In
How to update reordenated menu items in DB
#1

[eluser]Rubiz'[/eluser]
Good night all.

My clients ( they really sucks ), want have a possibility to reorder the main site menu, and submenu.

Ok.

I added a column in DB called show_order, and got this jquery link http://dev.jquery.com/view/tags/ui/lates...i.sortable.

Than, I serialized the list itens and send organized to form inputs called session[], so I have all items organized perfectly.

My doubt is about how to update existent show_order in DB...

If I have first:
1
2
3
4
5

Than the client changes it to:
2
4
5
3
1

...

Cant think a way to update database with it...

Any idea?
#2

[eluser]Rubiz'[/eluser]
Yes I'm really tired, working all day, I have solved the problem storing in hidden inputs called original_id[] the id to be changed.

Thanx for your attention in read it Smile
#3

[eluser]sikkle[/eluser]
Hi Rubiz,

i receive a lot of query about this process on irc, do you think it's possible for you to explain the process you use with more detail ?

thanks!
#4

[eluser]Rubiz'[/eluser]
My view:

Code:
[removed][removed]
[removed][removed]

[removed]
<!--
    $(document).ready(function(){
        $("#myList").sortable({});
      });
      
      function savePositions()
      {        
        order_str = $("#myList").sortable( "serialize" );
        split_order = order_str.split('&');
        
        rec_set = [];
        
        for (i=0; i < split_order.length; i++)
        {
            rec_set[i] =  split_order[i].split('=')[1];
        }
        
        for (i=0; i < rec_set.length; i++)
        {
            $('#thisForm').append("&lt;input type='hidden' name='session[]' value='" + rec_set[i] + "' /&gt;")
        }
        
        $('#thisForm').submit();
      }
--&gt;
[removed]

<center>
<h3 style="padding-bottom:30px;">REORDENANDO O MENU PRINCIPAL</h3>

&lt;form action="&lt;?=base_url()?&gt;index.php/session/reorder_submit" method="post" id="thisForm"&gt;

&lt;? $i = 0; ?&gt;

<ul id="myList">
&lt;? foreach( $session as $s ): ?&gt;

    &lt;? $i++ ?&gt;
    <li id="session_&lt;?=$i?&gt;">
        &lt;?=$this->Session_model->convert_id_to_title($s->id)?&gt;
        &lt;input type="hidden" name="original_id[]" value="&lt;?=$s-&gt;id?&gt;" />
    </li>

&lt;? endforeach ?&gt;
</ul>

&lt;input type="button" value="SALVAR POSIÇÕES"&gt;

&lt;/form&gt;

</center>

Controller:

Code:
function reorder_submit()
    {
        if ( $this->Session_model->reorder( $_POST ) ) $this->statusThis('1');
        else $this->statusThis('0');
    }

Model:

Code:
function reorder( $post )
    {
        for( $i=0; $i < sizeof($post['session']); $i++ )
        {
            $this->db->where( 'id', $post['original_id'][$i] );
            $this->db->update( 'sessions', array( 'show_order' => $i + 1 ) );
        }
        
        return true;
    }

I hope my CI design patterns are OK Smile
#5

[eluser]sikkle[/eluser]
thanks a lot for your time, that will help some.
#6

[eluser]gon[/eluser]
Transactions should be used here, to make sure the sorting operation is atomic, so info won't be corrupted if some query fails.
#7

[eluser]Rubiz'[/eluser]
Could u explain better Gon? I'd like to learn about this transactions. I really do remembered Hiroshima and Nagazaki with this "operation is atomic" rsrsrsrs.

What would be atomic sorting operaion?
#8

[eluser]gon[/eluser]
If you start doing updates for setting every item order, what if the last query fails?

Transactions allow you to write changes only if all queries work.

If you use transactions when writing the order of every item in different queries, all the queries will be considered as an atomic operation, because they are all either done or not. So you write all the updates, or you dont write any of them if there is an error.

check the codeigniter transactions user guide, its easy to implement them. But remember that in MySQL only InnoDB tables allow the use of transactions.




Theme © iAndrew 2016 - Forum software by © MyBB