CodeIgniter Forums
sortable divs jquery and codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: sortable divs jquery and codeigniter (/showthread.php?tid=12281)



sortable divs jquery and codeigniter - El Forum - 10-13-2008

[eluser]flashingback[/eluser]
Hello,

I am trying to make a sortable image list with jquery and CI. I've got the jquery part working but the php part doesn't want to work. It seems to update the mysql table but not right

This is wat I have:

first of al jquery gets the positioning of the divs I have sorted on update.

Code:
$("#sortable").sortable({
        handle: ".h_ico",
        update: function(){
        
                    stringDiv = "";
                    $("#sortable").children().each(function(i) {
                      var li = $(this);
                     if(li.attr("class") == "project"){
                      stringDiv += " "+li.attr("id") + '=' + i + '&';
                      }
                    });
    
                     $.ajax({
                       type: "POST",
                       url: "<?=base_url().'index.php/admin/updatelist'?>",
                       data: stringDiv,
                       success: function(msg){
                         alert( "Data Saved: " + msg );
                            [removed].reload( true );
                       }
                     });

                }
            
    });

So in the updatelist I receive something like this:

$_POST[0] = 1
$_POST[1] = 0
$_POST[2] = 2

updatelist method :

Code:
function updatelist () {
        
        $data =  $this->Projects->getList() ;
    
        foreach ( $_POST as $key => $value )
        {
            
            $newrank = $data[$key]->rank;
            $rank = $data[$value]->rank;
            if($rank != $newrank){
                $this->Projects->switchProject($rank,$newrank);
            }
            
        }
                
    }

and the switchProject method contains this (this probably contains the fault):

Code:
function switchProject ($rank,$newrank){
        
        $this->db->query("UPDATE gallery SET rank='".$newrank."' WHERE rank='".$rank."' ");
        
    }



sortable divs jquery and codeigniter - El Forum - 10-13-2008

[eluser]xwero[/eluser]
Can't you work with the id of the image? Then you can do
Code:
$this->db->query("UPDATE gallery SET rank='".$newrank."' WHERE id='".$id."' ");

I think the check to see if the list rank is changed should be dropped, if you just update each row the overhead will be more or less the same. You should benchmark it to see the results.


sortable divs jquery and codeigniter - El Forum - 10-13-2008

[eluser]flashingback[/eluser]
You are my hero, works like a charm