|
发表于 2018-5-8 17:01:03
|
显示全部楼层
order_by_field
本帖最后由 52lin 于 2018-5-8 17:04 编辑
你希望的是 按给定的这个【'理事长','副理事长','常务理事','理事'】顺序排序,即mysql 的 order by field('position','理事长','副理事长','常务理事','理事'),
貌似CI没有order_by_field这种方式,order_by也不支持种方式
你可以按管理说的那样去实现:
$sql = "select * from table where .... order by field('position','理事长','副理事长','常务理事','理事')";
$this->db-> query($sql);
当然你也可以自己去继承重写扩展这种方法order_by_field:
PHP复制代码
/**
* FIELD排序
*
* @param mixed $field 要排序的字段或者数组.
* @param mixed $array 排序的值,字符串或数组
*
* @example 1) order_by_field("`key`, 1,2,3")
* @example 2) order_by_field('key', '1,2,3')
* @example 2) order_by_field('key', array(1,2,3))
* @example 3) order_by_field(array('key' => '1,2,3', 'key2' => array(1,2,3)))
*
* @return object returns $this 用于链式操作.
*/
public function order_by_field ($field, $array = array(), $escape = NULL)
{
if ( ! empty($field))
{
if(is_string($field) && empty($array)){
$findex = strpos($field, ',');
if($findex !== false){
$array = substr($field, $findex+1);
$field = trim(substr($field, 0, $findex));
}
}
elseif (is_string($field) && is_array($array))
{
$tmp = array();
foreach ($array as $value) {
$tmp[] = "'".addslashes($value)."'";
}
$array = implode(', ', $tmp);
}
elseif (is_array($field) && empty($array))
{
foreach ($field as $f => $o)
{
if(is_array($o)){
$tmp = array();
foreach ($o as $value) {
$tmp[] = "'".addslashes($value)."'";
}
$this->order_by_field($f, implode(',', $tmp));
}else{
$this->order_by_field($f, $o);
}
}
}
if ( ! empty($field) && is_string($field) && ! empty($array) && is_string($array))
{
if ( ! in_array($field, $this->db->qb_aliased_tables))
{
$field = $this->db->protect_identifiers($field);
}
$field = ' FIELD ('.$field.', '.$array.')';
is_bool($escape) OR $escape = TRUE;
$orderby = array(array('field' => $field, 'direction' => '', 'escape' => $escape));
$this->db->qb_orderby = array_merge($this->db->qb_orderby, $orderby);
if ($this->db->qb_caching === TRUE)
{
$this->db->qb_cache_orderby = array_merge($this->db->qb_cache_orderby, $orderby);
$this->db->qb_cache_exists = array_merge($this->db->qb_cache_exists, array('orderby'));
}
}
}
return $this;
}//end order_by_field()
复制代码
扩展了之后就可以这样用了:
$this->db->order_by_field('position', array('理事长','副理事长','常务理事','理事'));
|
|