Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to do trim before inserting into database. I am using codeigniter . Now if i have to do trim before inserting , then i have to make code change across project . So i thought to do trim before insertion operation executed by codeigniter .

In system/database/drivers/mysql/mysql_driver.php in the _insert function i introduced a forloop to trim the data in array . The code is as follows .

function _insert($table, $keys, $values){
  $arr = array();
  foreach($values as $k => $v)
    $arr[$k] = trim($v);
  return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $arr).")";
}

Any guidance is appreciated . After inserting if i see the value database it is not trimmed. The same while i could see in the log file.

share|improve this question
    
It will better not to change the core class of the Codeigniter. Instead create a new class that extends the database class. You can then override the function accordingly. –  Akhilesh Sharma Jun 11 '13 at 12:40
    
most of the code is easy to update, however the DB classes are not designed like the core/libraries/helpers. Unless i missed something when i was using it. –  mic Jun 11 '13 at 14:50
2  
this is bad practice i advise not to hack directly to CI's core, no one will answer hacking onto the core ,when you can extend it or use hooks. or format and clean you're data before using the insert method. –  TomeӾsanS Jun 11 '13 at 15:45
    
thanks for the suggestion guys . i will check it out –  maxwells Jun 12 '13 at 5:14

2 Answers 2

up vote 1 down vote accepted

I could get this done from hooks class as supported by codeigniter.

Firstly enable hooks in application/config.php

Second application/config/hooks.php have added the following code .

$hook['pre_controller'] = array(
                            'function' => 'MyTrimer',
                            'filename' => 'utilities.php',
                            'filepath' => 'hooks'
                            );

Finally added the below code in , hooks/utilities.php

function MyTrimer(){
  if(count($_POST) > 0 ){
    foreach($_POST as $k => $v){
      if(!is_array($_POST[$k])){ 
        $_POST[$k]=trim($_POST[$k]);
      }   
    }   
  }
}
share|improve this answer

After posting in controller you could write trime before each post example code:

$this->load->model('test_model');
$form_data = array(
          'fname'   =>trim($this->input->post('first_name')),
          'lname'   =>trim($this->input->post('last_name')),
          'position'=>trim($this->input->post('position'))
           );
$this->test_model->insert('test_table',$form_data);
share|improve this answer
    
I could do that. But at lot of places , have insert scripts. SO cannot change across project. I have get this done by using hooks. –  maxwells Jun 15 '13 at 9:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.