在codeigniter使用排序 [英] Using Order By in codeigniter

查看:318
本文介绍了在codeigniter使用排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的正常MySQL查询:

This is my normal mysql query:

$sql = "SELECT * FROM item order by ";
if(my_condition)
{
       $sql. = "FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 
       'Desk &     Office', 'Bike &            Scooter', 'Tools', 'Leisure', 
        'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances',
        'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers', 
        'General Item')";

}
else
{
     $sql .= "category asc";
}

我需要它在CI中的活动记录。我试着下列方式:

I need it in CI in active record. I tried in following way:

      if(my_condition)
            {
                $this->db->order_by("FIELD(`category`, 'Auto & Accessories', 'Couch',
                'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron & 
                 Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & 
                 Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 
                 'TV, HIFI & Computers', 'General Item')");

            }
            else
            {
                $this->db->order_by("category", "asc");
            }

不过,我得到这个错误::

But, I got this error::

   Error Number: 1064
   You have an error in your SQL syntax; check the manual that corresponds to your           
   MySQL server version for the right syntax to use near 'Couch'`, `'Bed'`, `'Desk` &  
   Office', `'Bike` & Scooter', `'Tools'`, `'Leisure'`,' at line 6


  Filename: C:\xampp\htdocs\straatjutter_service\system\database\DB_driver.php

 Line Number: 330

如何解决呢?其实我需要我的项目的类别排序上面的格式。这可以在正常的MySQL查询来完成。但是,感到困惑,在做它的活动记录。

How to solve it? Actually I need my item's category sorted in above format. This can be done in normal mysql query. But, get confused in doing it in active records.

推荐答案

试试这个::

            $this->db->select('i.id, condition, description, created_at, updated_at, category, latitude, longitude, status, d.device_token, d.facebook, d.preferred, d.email, d.phone');
            $this->db->select("CONCAT('$url', `image_url_original`) AS `image_url_original`", false);
            $this->db->select("CONCAT('$url', `image_url_mid`) AS `image_url_mid`", false);
            $this->db->select("CONCAT('$url', `image_url_thumb`) AS `image_url_thumb`", false);
            $this->db->select("FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers', 'General Item') AS `sort_col`", false);

            $this->db->from('item as i');
            $this->db->join('device as d', 'i.device_id = d.device_token');

            $this->db->where('created_at > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR)');
            $this->db->where('i.status', 'shared');

            if($language === 'nl_NL') {
                $this->db->order_by('sort_col', 'asc');
            } else {
                $this->db->order_by('category', 'asc');
            } 

这篇关于在codeigniter使用排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆