title: CodeIgniter: set_value() or post() - what is faster and what is the best practice to store data into database
body: <p><strong>Background</strong>
  Using <code>Codeigniter</code> with <code>form_helper</code> and <code>form_validation</code> to do some form processing. <em>Form has been validated successfully</em> in the <code>controller</code>. </p>
  
  <p><strong>Now we need to put this data into the database using the <code>model</code> class.</strong></p>
  
  <p><strong>Assumptions</strong></p>
  
  <p>Lets assume our form has several input elements (e.g. >20).</p>
  
  <p><strong>Question</strong>
  Which of the following code snippets will be more efficient? <code>Both snippets are obviously inside the controller method to which the form submits data.</code></p>
  
  <p><em>Code Snippet 1</em></p>
  
  <pre><code>if ($this-&gt;form_validation-&gt;run())
  {
      // validation successful, now collect the values in a variable to pass it to the model.
      $form_data['field1'] = $this-&gt;form_validation-&gt;set_value('field1');
      $form_data['field2'] = $this-&gt;form_validation-&gt;set_value('field2');
      // AND SO ON
      $form_data['fieldN'] = $this-&gt;form_validation-&gt;set_value('fieldN');
  
      // Now put this data into database.
      $this-&gt;corresponding_model-&gt;write_to_db($form_data);
  }
  </code></pre>
  
  <p><em>Code Snippet 2</em></p>
  
  <pre><code>if ($this-&gt;form_validation-&gt;run())
  {
      // validation successful, now collect the values in a variable to pass it to the model.
      $form_data['field1'] = $this-&gt;input-&gt;post('field1');
      $form_data['field2'] = $this-&gt;input-&gt;post('field2');
      // AND SO ON
      $form_data['fieldN'] = $this-&gt;input-&gt;post('fieldN');
  
      // Now put this data into database.
      $this-&gt;corresponding_model-&gt;write_to_db($form_data);
  }
  </code></pre>
  
  <p><strong>So essentially what I am asking is:</strong> What is better to get the post data for some arbitrary form element? <code>$this-&gt;input-&gt;post</code> or <code>$this-&gt;form_validation-&gt;set_value()</code> ?</p>
  
  <p><strong>PS:</strong> If we look the the <code>set_value()</code> and <code>post()</code> functions in the code (please see below), obviously <code>set_value()</code> is going to be faster as <code>post()</code> loops through the entire <code>$_POST</code>. So in a sense it is also about what is the best practice?</p>
  
  <p><strong>Form_validation.php, set_value() method</strong></p>
  
  <pre><code>public function set_value($field = '', $default = '')
  {
      if ( ! isset($this-&gt;_field_data[$field]))
      {
          return $default;
      }
  
      // If the data is an array output them one at a time.
      //     E.g: form_input('name[]', set_value('name[]');
      if (is_array($this-&gt;_field_data[$field]['postdata']))
      {
          return array_shift($this-&gt;_field_data[$field]['postdata']);
      }
  
      return $this-&gt;_field_data[$field]['postdata'];
  } 
  </code></pre>
  
  <p><strong>Input.php, post() method</strong></p>
  
  <pre><code>function post($index = NULL, $xss_clean = FALSE)
  {
      // Check if a field has been provided
      if ($index === NULL AND ! empty($_POST))
      {
          $post = array();
  
          // Loop through the full _POST array and return it
          foreach (array_keys($_POST) as $key)
          {
              $post[$key] = $this-&gt;_fetch_from_array($_POST, $key, $xss_clean);
          }
          return $post;
      }
  
      return $this-&gt;_fetch_from_array($_POST, $index, $xss_clean);
  }
  </code></pre>