Welcome Guest, Not a member yet? Register   Sign In
Utility code snippets - share yours
#1

[eluser]nuwanda[/eluser]
Hi, All

Thought I'd share two utility snippets I use. Nothing too original but they may help someone.

Feel free to share yours. The mod may sticky this.

This one gets a field (what) from (where) containing (where_value).

Code:
function get_what_where($what, $where, $where_value, $table){
      $this->db->select($what);
      $this->db->where($where,$where_value);
      $query=$this->db->get($table);
    
      if($query->num_rows==1){
          return $query->row($what);
      }else
        return 0;
      }
    }

This updates a field (what) with (what_value) from (where) containing (where_value).
Code:
function put_what_where($what,$what_value,$where,$where_value,$table){
      $this->db->set($what,$what_value);
      $this->db->where($where,$where_value);
    
      if($this->db->update($table)){
        return 1;
      }
      return 0;
    }

Both return either success or failure.
#2

[eluser]Vega[/eluser]
Heres one I wrote that tests for bad words in a string, (you need a table of bad words):

Code:
public function bad_words($str, $seperators = array('-', '_'))
{
    $CI =& get_instance();
    $haystack = strtolower(str_replace($seperators, '', $str));
    $query = $CI->db->get('bad_words');
    
    foreach ($query->result_array() as $row)
    {
        if (strpos($haystack, $row['word']) !== FALSE)
        {
            return FALSE;
        }
    }
    
    return TRUE;
}




Theme © iAndrew 2016 - Forum software by © MyBB