title: codeigniter jquery sortable save to database
body: <p>I am trying to save the order of a list of images to the database when using jquery sortable. 
  I feel i am very close, but cant get my head around the final details.
  I am working with CI 2.1.3 and jquery-ui 1.10.3.</p>
  
  <p>I have a dynamicaly generated list with an image:</p>
  
  <pre><code>&lt;ul id="order"&gt;
  &lt;li id="item-1"&gt;&lt;img src="abc.jpg" /&gt;&lt;/li&gt;
  &lt;li id="item-2"&gt;&lt;img src="def.jpg" /&gt;&lt;/li&gt;
  &lt;li id="item-3"&gt;&lt;img src="ghi.jpg" /&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>And the following Jquery:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function() {
  
          $( "#order" ).sortable({
              opacity: 0.6,
              cursor: 'move',
  
              update: function(event, ui){
   var order = $(this).sortable("serialize");
   console.log(order);
  
                  $.ajax({
                      url: "http://localhost/gridrobin/home/save_order",
                      type: 'POST',
                      data: order,
                      success: function (data) {
                          $("#test").html(data);
                      }
  
                  });
                  }
  
              });
  
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>This works fine and i can reorder my list. Now i want to save the new order to the database. I send the ajax post to the controller and it comes through. I checked with a var_dump.</p>
  
  <pre><code>//var_dump($_POST);
  
          $items = $this-&gt;input-&gt;post('item');
          $total_items = count($this-&gt;input-&gt;post('item'));
  
          echo '&lt;h3&gt;Debugging&lt;/h3&gt;';
          echo "&lt;p&gt;Total items sent: $total_items&lt;/p&gt;";
  
          $this-&gt;rd_model-&gt;update_order($total_items, $items);
  </code></pre>
  
  <p>Then I send this data to my model:</p>
  
  <pre><code> for($item = 0; $item &lt; $total_items; $item++ )
          {
  
              $data = array(
                      'id' =&gt; $items[$item],
                      'order' =&gt; $order = $item
              );
  
              $this-&gt;db-&gt;where('id', $data['id']);
  
              $this-&gt;db-&gt;update('portfolio_items', $data);
              echo '&lt;br /&gt;'.$this-&gt;db-&gt;last_query();
          }
  </code></pre>
  
  <p>And echo out the last db-query for debugging. </p>
  
  <p>Now when i switch item 1 and item 2, i get a 500 internal error. When i switch them back, i receive the echo of the last query executed, which seems fine.</p>
  
  <pre><code>UPDATE `portfolio_items` SET `order` = 1 WHERE `id` = '1'
  UPDATE `portfolio_items` SET `order` = 2 WHERE `id` = '2'
  UPDATE `portfolio_items` SET `order` = 3 WHERE `id` = '3'
  </code></pre>
  
  <p>I dont quite understand why the database updates when th list is switched back to its orignial state, but not otherwise. </p>
  
  <p><strong>UPDATE</strong>
  For people with the same problem, sakibmoon answer helped me a lot, but the main problem was a duplicate entry error, because apparently i had set the order table as a unique index...</p>