Skip to content

Instantly share code, notes, and snippets.

@michaelfox
Created February 22, 2013 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelfox/5016631 to your computer and use it in GitHub Desktop.
Save michaelfox/5016631 to your computer and use it in GitHub Desktop.
Function for MY_Model that allows you to do FIND_IN_SET() Ordering via the CodeIgniter Active Record Class.
<?php
/**
* http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_find-in-set
*
* @param field
* @param first_value, second_value, ...
*/
public function sort_literal() {
$args = func_get_args();
$field = $this->db->protect_identifiers(array_shift($args));
foreach ($args as &$val) {
$val = $this->db->escape_str(trim($val));
}
$set_str = join($args, ',');
$set_str = str_replace(" ", "", $set_str);
$orderby_statement = "FIND_IN_SET($field, '{$set_str}')";
$this->db->ar_orderby[] = $orderby_statement;
if ($this->db->ar_caching === TRUE) {
$this->db->ar_cache_orderby[] = $orderby_statement;
$this->db->ar_cache_exists[] = 'orderby';
}
return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment