Welcome Guest, Not a member yet? Register   Sign In
Moveup & Movedown functions using datamapper
#1

[eluser]Andy78[/eluser]
I have a simple table layout with items that are sortable by clicking on little up and down arrows at the end of the row. We've all seen them in cms's etc...

I am using datamapper with this application

here is my moveup function in the controller:

Code:
//menu item move up function
    function moveup($menu_id, $item_id){
        
        $current = new Menu_item();    
        $current->where('id', $item_id)->get();
        $new = $current->order - 1;
        
        $moveup = new Menu_item();
        $moveup->where('menu_id', $menu_id);
        $moveup->where('order', $new)->get();
        $order = $moveup->order;
        $moveup->order = $order + 1;
        
        $movedown = new Menu_item();
        $movedown->where('id', $item_id)->get();
        $order2 = $movedown->order;
        $movedown->order = $order2 - 1;
        
        if($moveup->save() && $movedown->save()){
            
                $this->session->set_flashdata('success', 'Menu Item Moved up successfully');
                redirect('menu_manager/menu_items/'.$menu_id);                  
            
        }
        else{
                $this->session->set_flashdata('errors', 'Error: Menu Item could not be moved!');
                redirect('menu_manager/menu_items/'.$menu_id);              
        
        }
          
    }

This is how Iv done it before using ordinary php

Code:
function moveup($id,&$db)
{
$current = $db->get_var("SELECT intOrder FROM tblMenuItems WHERE intItemID = ".$db->mysql_prep($id));
$new = $current - 1;
$sql = "UPDATE tblMenuItems SET intOrder = intOrder + 1 WHERE intOrder = $new AND intMenuID = ".$db->mysql_prep($_GET['_menuid']);
$db->query($sql);
$sql = "UPDATE tblMenuItems SET intOrder = intOrder - 1 WHERE intItemID = ".$db->mysql_prep($id);
$db->query($sql);
}



I have the reverse for movedown but surly there is a more efficient way of doing this with datamapper? I know I could use fancy jquery drag n drop ect but just trying to keep this simple.
#2

[eluser]WanWizard[/eluser]
I would just use a normal UPDATE query, from a method in the model, so you can do $model->moveup(), which moves the current object one up, and updates the objects contents.
#3

[eluser]Andy78[/eluser]
Ok makes sense.




Theme © iAndrew 2016 - Forum software by © MyBB