CodeIgniter Forums

Full Version: sortable divs jquery and codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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."' ");
        
    }

El Forum

[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.

El Forum

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