CI_DB_active_record::order_by PHP Method

order_by() public method

Sets the ORDER BY value
public order_by ( $orderby, $direction = '' ) : object
return object
    function order_by($orderby, $direction = '')
    {
        if (strtolower($direction) == 'random') {
            $orderby = '';
            // Random results want or don't need a field name
            $direction = $this->_random_keyword;
        } elseif (trim($direction) != '') {
            $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' ' . $direction : ' ASC';
        }
        if (strpos($orderby, ',') !== FALSE) {
            $temp = array();
            foreach (explode(',', $orderby) as $part) {
                $part = trim($part);
                if (!in_array($part, $this->ar_aliased_tables)) {
                    $part = $this->_protect_identifiers(trim($part));
                }
                $temp[] = $part;
            }
            $orderby = implode(', ', $temp);
        } else {
            if ($direction != $this->_random_keyword) {
                $orderby = $this->_protect_identifiers($orderby);
            }
        }
        $orderby_statement = $orderby . $direction;
        $this->ar_orderby[] = $orderby_statement;
        if ($this->ar_caching === TRUE) {
            $this->ar_cache_orderby[] = $orderby_statement;
            $this->ar_cache_exists[] = 'orderby';
        }
        return $this;
    }

Usage Example

Example #1
0
 public function setup_order(CI_DB_active_record $db)
 {
     $db->join('annotation2scope AS order_annotation2scope', 'order_annotation2scope.annotation_id = annotation.annotation_id');
     $db->join('scope AS order_scope', 'order_scope.scope_id = order_annotation2scope.scope_id');
     if ($this->desc === TRUE) {
         $db->select('order_scope.to_index');
         $db->order_by('order_scope.to_index', $this->get_direction());
     } else {
         $db->select('order_scope.from_index');
         $db->order_by('order_scope.from_index');
     }
     return $db;
 }
All Usage Examples Of CI_DB_active_record::order_by