Welcome Guest, Not a member yet? Register   Sign In
Order BY not working
#7

[eluser]Larry Wakeman[/eluser]
The code that is causing the issue is in a CRUD model, extended by table and view specific models.

Code:
function getAll($a_where = '', $a_order = null)
    {
        $a_data = Array();
        if ($a_where != '')
        {
            $o_query = $this->db->where($a_where);
        }
        if (!is_null($a_order))
        {
            if (is_array($a_order))
            {
                foreach($a_order as $a_entry => $s_order);
                {
                    $o_query = $this->db->order_by($a_entry, $s_order);
                }
            } else {
                $o_query = $this->db->order_by($a_order, 'asc');
            }
        }
        $o_query = $this->db->get($this->s_table);
        if ($o_query->num_rows() > 0)
        {
            foreach ($o_query->result_array() as $a_row)
            {
                $a_data[] = $a_row;
            }
        }
        $o_query->free_result();
        return $a_data;
    }

The call is:
Code:
$this->a_items[$a_menu['id']] = $this->o_CI->menu_items->getAll(array('menuid' => $a_menu['id']), array('level'=> 'asc', 'parentid' => 'asc', 'order' => 'asc');

When I was debugging the code, I noticed that the sorting was by the last element in the ordering array. I dumped the $o_query object just before the get and verified that the last element was the only one that was in the object.

I created a workaround as follows:
Code:
function getAll($a_where = '', $a_order = null)
    {
        $a_data = Array();
        if ($a_where != '')
        {
            $o_query = $this->db->where($a_where);
        }
        if (!is_null($a_order))
        {
            if (is_array($a_order))
            {
                $s_order = '';
                foreach($a_order as $s_entry => $s_dir)
                {
                    $s_order .= $s_entry.' '.$s_dir.', ';
                }
                if ($s_order != '') $o_query = $this->db->order_by(substr($s_order, 0, strlen($s_order) -2));
            } else {
                $o_query = $this->db->order_by($a_order, 'asc');
            }
        }
        $o_query = $this->db->get($this->s_table);
        if ($o_query->num_rows() > 0)
        {
            foreach ($o_query->result_array() as $a_row)
            {
                $a_data[] = $a_row;
            }
        }
        $o_query->free_result();
        return $a_data;
    }
This is working.

I may be naive but I was programming Fortran II on an IBM 1130 before most of the members of this forum were twinkles in their father's eyes and for many before their father was a twinkle... :-) and I have some silly notions that documentation should describe the real world, applications don't break computers, removing applications don't break computers.


Messages In This Thread
Order BY not working - by El Forum - 11-14-2010, 11:47 AM
Order BY not working - by El Forum - 11-14-2010, 11:52 AM
Order BY not working - by El Forum - 11-14-2010, 12:08 PM
Order BY not working - by El Forum - 02-19-2011, 01:44 PM
Order BY not working - by El Forum - 02-19-2011, 03:00 PM
Order BY not working - by El Forum - 02-19-2011, 06:30 PM
Order BY not working - by El Forum - 02-20-2011, 10:48 AM
Order BY not working - by El Forum - 02-20-2011, 12:36 PM
Order BY not working - by El Forum - 02-20-2011, 07:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB