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

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:

//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');
                $this->session->set_flashdata('errors', 'Error: Menu Item could not be moved!');

This is how Iv done it before using ordinary php

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']);
$sql = "UPDATE tblMenuItems SET intOrder = intOrder - 1 WHERE intItemID = ".$db->mysql_prep($id);

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.

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.

Ok makes sense.

Theme © iAndrew 2016 - Forum software by © MyBB