This project is read-only.

Save order in Mysql

Apr 1, 2012 at 5:23 PM

Thx for this script was looking for this a few days. I would like to save the order in database. Moving, saving order works and it saves these parameters(checked with firebug) to update.php:

ids[] 2
ids[] 1
ids[] 3
ids[] 6
ids[] 4
ids[] 5
ids[] 7
ids[] 8
ids[] 9
ids[] 10
ids[] 11
ids[] 12
Source
ids%5B%5D=2&ids%5B%5D=1&ids%5B%5D=3&ids%5B%5D=6&ids%5B%5D=4&ids%5B%5D=5&ids%5B%5D=7&ids%5B%5D=8&ids%5B%5D=9&ids%5B%5D=10&ids%5B%5D=11&ids%5B%5D=12

Somehow it doesn't update the database. Can you look into this. update.php:

<?php
include('instellingen.php');  
$ip = $_SERVER['REMOTE_ADDR'];
$ids = explode("|", $_POST["ids"]);
for ($idx = 0; $idx < count($ids); $idx+=1) {
  $id = $ids[$idx];
  $ordinal = $idx;
  mysql_query("UPDATE orderblok SET sort_no = " . $idx . ", ip = " . $ip . " WHERE id = " . (int)$ids[$idx]);
}
?>

Jquery code:
            $(".jk").dragsort({ dragSelector: "div", dragEnd: saveOrder, placeHolderTemplate: '<div class="blokhl"></div>' });

            function saveOrder() {
                var data = $(".jk .blok").map(function() { return $(this).data("itemid"); }).get();
                $.post("f/update.php", { "ids[]": data });
            };

Mysql table:
INSERT INTO `orderblok` (`id`, `sort_no`, `ip`) VALUES
(1, 1, '0'),
(2, 2, '0'),
(3, 3, '0'),
(4, 4, '0'),
(5, 5, '0'),
(6, 6, '0'),
(7, 7, '0'),
(8, 8, '0'),
(9, 9, '0'),
(10, 10, '0'),
(11, 11, '0'),
(12, 12, '0'); 

Cyto
Apr 21, 2012 at 2:55 AM

Did you resolve that ?

Apr 21, 2012 at 6:48 AM
Edited Apr 21, 2012 at 8:28 AM

Hi Cyto apologies for the delay (I did get your email letting me know of this post, good idea, but left it to when I had spare time and forgot).

I think you will find $_POST["ids"] is already an array and there's no need to call explode on it. Originally in my examples I used to join the ids together separated by | as a single string and post that to the server where I would split the string back into an array. Then later I discovered you can post arrays to the server without the need to convert into a string and back again so changed my examples.

Just so you know in my example I post the array as { "ids[]": data } to support older versions of jquery (before v1.5 I think) but you now can write { ids: data }.

Mar 16, 2013 at 1:35 AM
Hi,

I continue this post because its a similar problem.

First, great script. Work great.
The problem is that I just cant understand how to save the reordered list.

I have a mysql table to order an image gallery. this is the structure after uploading the files:
ID  ORDEN   LINK
1   1           1_MG_8870.jpg           
2   2           0_MG_9260.jpg           
3   3           1_MG_9335.jpg           
4   4           2_MG_9337.jpg           
Since I want to be able to change the order I'm using this script
<ul id="list1">
<?
                            
                                while($articulo=mysql_fetch_array($resultados)) {
                                                
            echo '<li data-itemid="'.$articulo["ORDEN"].'"><div><img src="multi/'.$articulo["LINK"].'" width="120" height="120" /></div></li>';
            }
    ?>
    
    </ul>
<input name="list1SortOrder" type="text" size="244" />
<script type="text/javascript">
        $("#list1").dragsort({ dragSelector: "div", dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

           function saveOrder() {
            var data = $("#list1 li").map(function() { return $(this).data("itemid"); }).get();
            $("input[name=list1SortOrder]").val(data.join("|"));
            $.post("fotoalbum.php", { "ids": data });
        };
    </script>
I set the input to be visible and reflect the change every time I drag&drop a picture (ej: 0|1|2|3|4 and changing... ), but I'm not being able to insert this new order to de database.

Sorry if its too dumb the problem.....

THAnks!