code2 formelement generator


SUBMITTED BY: phpsnippets

DATE: Oct. 24, 2015, 10:24 a.m.

FORMAT: Text only

SIZE: 3.5 kB

HITS: 3702

  1. <?php
  2. /**
  3. * @name: set_attribute
  4. * Create attributes for DOM element from array. The html5 data-attributes and native attributes are hadnled separately
  5. * @param $attributes
  6. * @param null $data_attributes
  7. * @return string
  8. */
  9. function set_attributes($attributes, $data_attributes = null){
  10. $html = ' ';
  11. foreach($attributes as $k => $v){
  12. if(!is_array($v)){
  13. $html.=$k.'="'.$v.'" ';
  14. }
  15. }
  16. if($data_attributes && $data_attributes !== null && is_array($data_attributes)){
  17. foreach($data_attributes as $k => $v){
  18. if(!is_array($v)){
  19. $html.='data-'.$k.'="'.$v.'" ';
  20. }
  21. }
  22. }
  23. return $html;
  24. };
  25. /**
  26. * @name build_selector
  27. * Creates a html select element with options, optgroups. It marks the selected element or if
  28. * selected element is not presented, the first element.
  29. * @param array $options Source for options and optgroups
  30. * Format:
  31. * <b>options:</b>
  32. * - value => option
  33. * <b>groups:</b>
  34. * - group => array( 'groupname', array( //options )
  35. * @param null $attributes Source for attributes
  36. * @param null $selected Value of the selected option
  37. * @throws Exception
  38. */
  39. function build_selector($options = array(),$attributes = null,$selected = null){
  40. $options = (!empty($options) && is_array($options)) ? $options : false;
  41. $selected = ($selected !== null && !empty($selected) && !is_array($selected)) ? $selected : false;
  42. $attributes = (!empty($attributes) && is_array($attributes)) ? $attributes : false;
  43. $data_attributes = (array_key_exists('data-',$attributes) && (!empty($attributes['data-']))) ? $attributes['data-'] : false;
  44. $html_wrapper = '<select';
  45. $html_inside = '';
  46. if(!$options){ throw new Exception('options argument must be an array!'); exit(1);}
  47. function create_options($options,$selected, $i = null){
  48. $i = ($i !== null) ? $i : 0;
  49. foreach($options as $k => $v){
  50. $hasChild = ( $k !== null && strtolower($k) == 'group' && is_array($v)) ? true : false;
  51. if(!$hasChild){
  52. if($v && !empty($v))
  53. echo '<option value="'.$k.'"'.(($selected && $selected == $k)||(!$selected && $i == 0)).'>'.$v.'</option>';
  54. else
  55. echo '<option value="'.$k.'"'.(($selected && $selected == $k)||(!$selected && $i == 0)).'>'.$k.'</option>';
  56. $i++;
  57. }else{
  58. echo '<optgroup label="'.$v[0].'">';
  59. call_user_func('create_options',$v[1], $i);
  60. echo '</optgroup>';
  61. }
  62. }
  63. };
  64. try{
  65. var_dump($data_attributes);
  66. $attributes_html = set_attributes($attributes,$data_attributes);
  67. echo '<select'.$attributes_html.'>';
  68. create_options($options,$selected);
  69. echo '</select>';
  70. } catch (Exception $e){
  71. echo "build_selector error: ".$e->getMessage();
  72. die(1);
  73. }
  74. };
  75. ?>

comments powered by Disqus