CodeIgniter Helper Function: set_muli_value


/ Published in: PHP
Save to your folder(s)

CodeIgniter Helper function to repopulate form values when a muli-dimensional post array key is used.


Copy this code and paste it in your HTML
  1. if (!function_exists('set_multi_value')) {
  2.  
  3. /**
  4.   * Repopulate form values when a muli-dimensional post array key is used.
  5.   *
  6.   * @param string $field
  7.   * @param string $index
  8.   * @param string $default
  9.   * @return string
  10.   */
  11. function set_multi_value($field, $index=null, $default=null) {
  12. $CI = & get_instance();
  13. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  14. if (preg_match('/^([a-z0-9_-]+)\[([0-9]+)\]$/i', $field, $matches)) {
  15. return $CI->security->xss_clean($_POST[$matches[1]][$matches[2]]);
  16. } elseif (!is_null($index)) {
  17. return $CI->security->xss_clean($_POST[$field][$index]);
  18. } elseif (is_null($default)) {
  19. return $CI->security->xss_clean($_POST[$field]);
  20. } else {
  21. return $CI->security->xss_clean($default);
  22. }
  23. } elseif (!is_null($default)) {
  24. return $CI->security->xss_clean($default);
  25. } else {
  26. return '';
  27. }
  28. }
  29.  
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.