Welcome Guest, Not a member yet? Register   Sign In
Sorting a query
#1

[eluser]Maximilian Schoening[/eluser]
Hi,
how can I easily sort the output of this query. I would like to keep using an "active record" function and not my own SQL query:

Code:
function myfunction($id, $count, $offset) {
        $this->CI =& get_instance();
        
        $query = $this->CI->db->get_where('table', array('action' => $this->CI->auth->get_id($id)), (int)$count, (int)$offset);
        
        if ($query->num_rows() < 1) {
            return FALSE;
        }
        
        return $query->result_array();
}

Thanks,
Max
#2

[eluser]umefarooq[/eluser]
you can sort you records by following ways in active recods

Code:
by default asc sort
$this->db->order_by('desired field')

if you want desc sort
$this->db->order_by('desired field','desc')

if you want random

$this->db->order_by('desired field','random')

check user guide also

http://ellislab.com/codeigniter/user-gui...ecord.html
#3

[eluser]Maximilian Schoening[/eluser]
Thanks for the fast reply. Ok so I am doing this:

Code:
function get($email, $count, $offset) {
        $this->CI =& get_instance();
        
        $user_id = $this->CI->auth->get_id($email);
        
        $query = $this->CI->db->order_by('id', 'desc')->get_where('uploads', array('user' => $user_id), (int)$count, (int)$offset);
        
        if ($query->num_rows() < 1) {
            return FALSE;
        }
        
        return $query->result_array();
    }

This works fine but somehow if I don't pass the user_id to the $user_id variable but do this directly:

Code:
function get($email, $count, $offset) {
        $this->CI =& get_instance();
        
        $query = $this->CI->db->order_by('id', 'desc')->get_where('uploads', array('user' => $this->CI->auth->get_id($email)), (int)$count, (int)$offset);
        
        if ($query->num_rows() < 1) {
            return FALSE;
        }
        
        return $query->result_array();
    }

It stops working. Any ideas?

Thanks,
Max
#4

[eluser]umefarooq[/eluser]
for that you have to print you query to check it proper query and parameter you are passing is correct or not for that use the following helper function

Code:
$this->db->last_query();

it will print your sql which you can use to check query it fine or not

http://ellislab.com/codeigniter/user-gui...lpers.html




Theme © iAndrew 2016 - Forum software by © MyBB