Php jqueryui可排序图像

Php jqueryui可排序图像,php,jquery,jquery-ui,codeigniter,jquery-ui-sortable,Php,Jquery,Jquery Ui,Codeigniter,Jquery Ui Sortable,您好,我需要一些帮助,因为我在jQuery方面的技能不太好。我想要实现的是改变图像的顺序,如示例所示 我的数据库如下所示: table: Gallery img_id (pk) image caption order 我还创建了以下两个视图: index.php <!-- will display the ajax result --> <div id="orderResult"></div> <hr/> <input type="bu

您好,我需要一些帮助,因为我在jQuery方面的技能不太好。我想要实现的是改变图像的顺序,如示例所示

我的数据库如下所示:

table: Gallery
img_id (pk)
image
caption
order  
我还创建了以下两个视图: index.php

<!-- will display the ajax result -->
<div id="orderResult"></div>
<hr/>
<input type="button" id="save" value="Save Order" class="btn btn-primary">

<script type="text/javascript">
    $(function () {
        $.post('<?php echo site_url('admin/galleries/order_ajax'); ?>', {}, function(data) {
            $('#orderResult').html(data);
        });

        // save order
       $('#save').click();
    });
</script> 
<?php if(count($images)>0): ?>
<div class="sortable-list">
    <ol id="sortable">
        <?php foreach($images as $image): ?>
        <li <?php echo 'id="li_'.html_escape($image->img_id).'"'; ?>><?php echo img(array('src' => 'uploads/thumbs/'.html_escape($image->image)); ?></li>
        <?php endforeach; ?>
    </ol>
</div>  
<?php endif; ?>

<script type="text/javascript">
    $(function() {
         $( "#sortable" ).sortable();
         $( "#sortable" ).disableSelection();
    });
</script> 

因此,我基本上想做的是拖动图像以改变它们的顺序,当我单击save按钮时,将(新的)数据/顺序传递给控制器,并将它们存储在数据库中。我怎样才能做到这一点呢?

嗯,解决方案比我想象的要简单

在index.php视图中,我将

<script type="text/javascript">
    $(function () {
        $.post("<?php echo site_url('admin/galleries/order_ajax'); ?>", {}, function(data) {
            $('#orderResult').html(data);
        });

        // save order
        $('#save').click(function(){
            var new_order = $("#sortable").sortable( "toArray" );

            $.post("<?php echo site_url('admin/galleries/order_ajax'); ?>", { order_array: new_order }, function(data) {
                    $('#orderResult').html(data);
                });
        });  
    });
</script>

通过AJAX将JSON发送到PHP文件(控制器+方法),并将订单保存到数据库。我不知道sortable插件会返回什么样的数据。我做了更新,请查看。
<script type="text/javascript">
    $(function () {
        $.post("<?php echo site_url('admin/galleries/order_ajax'); ?>", {}, function(data) {
            $('#orderResult').html(data);
        });

        // save order
        $('#save').click(function(){
            var new_order = $("#sortable").sortable( "toArray" );

            $.post("<?php echo site_url('admin/galleries/order_ajax'); ?>", { order_array: new_order }, function(data) {
                    $('#orderResult').html(data);
                });
        });  
    });
</script>
public function order_ajax(){
    // save order from pages
    if (isset($_POST['order_array'])) {
        $this->gallery_model->save_order($_POST['order_array']);
    }

    // fetch all images
    $this->data['images'] = $this->gallery->get();

    // load the view
    $this->load->view('admin/gallery/order_ajax', $this->data, false);
}