拖放& amp;数据库更新

时间:2014-10-21 14:31:13

标签: database list drag-and-drop sql-update

我有一个类别列表,我想使用拖放来编辑他们的订单。到目前为止,我有这个代码并且它有效,但我已经失去了很多时间,我没有弄清楚如何用他们的新订单更新数据库:

<ul id="categoryorder">
  <?php foreach ($categories as $category) : ?>
     <li id="<?php echo($category->order_id); ?>">
       <form enctype="multipart/form-data" style="margin-bottom: 0;" action="<?php echo base_url();?>backend/cp/categories" method="post">
          <input type="hidden" name="c_img" value="<?php echo($category->c_img); ?>" />
          <input type="hidden" name="id" value="<?php echo($category->id); ?>" />
          <input type="text" name="name" value="<?php echo($category->c_name); ?>" style="width:150px;" />
          <input type="file" name="c_img" size="20" value="" />
          <input type="checkbox" name="delete" value="1" />
          <input type="submit" name="update_category" value="Save" />
       </form>
     </li>
  <?php endforeach; ?>
</ul>

和js:

<script type="text/javascript">
$(document).ready(function(){

    $("ul#categoryorder").sortable({
        opacity: 0.6,
        cursor: 'move'
    });

});
</script>

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

$( "#categoryorder" ).sortable({
        forcePlaceholderSize: true,
        forceHelperSize: true,
        dropOnEmpty: false,
        scrollSensitivity: 100,
        scrollSpeed: 0,
        tolerance: "pointer",
        zIndex: 9999,
        stop: function(event,ui){
            var i = 0;
            var positions = '';
            var ids = '';
            $('#categoryorder li').each(function(){
                positions += i++ + ',';
                ids += $(this).attr('id') + ',';
            });
            $.ajax({
                type:'POST',
                data:'positions=' + positions + '&ids=' + ids,
                url: 'your url',
                async: false
            });

        }
    });

和php代码:

$positions   = explode(',', $_POST['positions']);
$ids         = explode(',', $_POST['ids']);

if (!empty($positions) && !empty($ids)) {
            foreach($positions as $value => $position_id) {
                if ($ids[$value] != '' && $position_id != '') {
                    $id = $ids[$value];
                    $position_id;
                }
            }
        }