Order BY not working

#1
[eluser]Thiago Leao[/eluser]
Hi friends,

I'm trying to Sort a list, but not working.
Code:
<?php
class Home_model extends Model{

    function listar_category(){    
    
        $query = $this->db->get('category');
        $this->db->orderby("id_category", "asc");
        return $query->result();
    }
    
}
?>

I know there must be something very simple, but I'm not getting.
I'm new at CI, i'm sorry!

thanks

#2
[eluser]Georgi Veznev[/eluser]
[quote author="Thiago Leao" date="1289778449"]Hi friends,

I'm trying to Sort a list, but not working.
Code:
<?php
class Home_model extends Model{

    function listar_category(){    
    
        $query = $this->db->get('category');
        $this->db->orderby("id_category", "asc");
        return $query->result();
    }
    
}
?>

I know there must be something very simple, but I'm not getting.
I'm new at CI, i'm sorry!

thanks[/quote]


Code:
<?php
class Home_model extends Model{

    function listar_category(){    
        $this->db->order_by("id_category", "asc");
        $query = $this->db->get('category');
        return $query->result();
    }
    
}
?>

#3
[eluser]Thiago Leao[/eluser]
so simple!
=/
i'm sorry.
thanks friend

#4
[eluser]Larry Wakeman[/eluser]
I am having another problem with order by. The documentation states:

Quote:Or multiple function calls can be made if you need multiple fields.

$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc");

// Produces: ORDER BY title DESC, name ASC

This does not seem to be the case. I did a print_r on the query object and only the last call is there, the others are overwritten.

I am using CI Version 1.7.2 at the moment. Thought about upgrading to 1.7.3 but when I looked, there were no translations for CI and localization is high on list of requirements for this project.

#5
[eluser]Georgi Veznev[/eluser]
@Larry Wakeman
Please, post the function that isn't working?

#6
[eluser]InsiteFX[/eluser]
orderby has been replaced with order_by !

Also do not use double qoutes, use single qoutes.

InsiteFX

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

#8
[eluser]Georgi Veznev[/eluser]
Here is the code that is causing the problems.
I think that this should work:

Code:
function getAll($a_where = '', $a_order = null) {
    $a_data = Array();

    if ($a_where != ''){
        $this->db->where($a_where);
    }
    if (!is_null($a_order)){
        if (is_array($a_order)){
            foreach($a_order as $a_entry => $s_order){
                $this->db->order_by($a_entry, $s_order);
            }
        } else {
            $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;
}

#9
[eluser]Larry Wakeman[/eluser]
No joy! The order by clauses are being overwritten by the order_by call. The last one id the only one being sorted by.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.