Monday, November 19, 2012

Collection : PHP : select_tag

Learning PHP is fun to me but really miss the available helpers in Rails.
For example form helpers like form/ select_tag/ text_field and others.

I dont know whether these are already available in php or not.
So I added one file as common_helper.php and that can be directly required or just added separate (module_name)_helper.php and require common_helper.php in the new file. I have added module wise view helpers, so that I can keep the common helper functions and module wise helper function separate from each other.

This function creates a HTML select with options and selected option. Parameters required are :
  1. $name as string (Name for select node).
  2. $options as array (Array for generating options).
  3. $selected as string or integer - optional - (Selected value or blank string).
  4. $css_class as string - optional - (Css class name(s)).
  5. $onchange as string - optional - (onchange functionality)
This will append the given name and class name to select tag. Selected value will be selected in options by comapering the $selected.
Options send to the helper function needs to be array.
For example:
1. array(1, 2, 3, 4, 5, 6, 7);
Or
2. array(array(0,"0 Hours"), array(1,"1 Hour"), array(2,"2 Hours"), array(3, "3 Hours"), array(4, "4 Hours"));
Still for making work this code you will need to pass first 2 mandatory parameters which are name and array of options which will create the option tags for select_tag.

  1. <?php 
  2.   /**
  3.   * Creates a HTML select with options and selected option.
  4.   * @param string $name <p>Name for select node</p>
  5.   * @param array $options <p>Array for generating options</p>
  6.   * @param string $selected <p>Selected value or blank string. Can be string or integer.</p>
  7.   * @param string $css_class <p>Css class name(s) as a string</p>
  8.   * <p>
  9.   * This will append the given name and class name to select tag.
  10.   * Selected value will be selected in options by comapering the $selected.
  11.   * Options send to the helper function needs to be array
  12.   * For example:
  13.   * 1. array(1, 2, 3, 4, 5, 6, 7);
  14.   * Or
  15.   * 2. array(array(0,"0 Hours"), array(1,"1 Hour"), array(2,"2 Hours"), array(3, "3 Hours"), array(4, "4 Hours"));
  16.   * </p>
  17.   */
  18.   function select_tag($name, $options, $selected="", $css_class="", $onchange=""){
  19.     $select_tag = '<select class="' . $css_class . '" name="'.$name;
  20.     if($onchange!=""){
  21.       $select_tag .= '" onchange="'.$onchange;
  22.     }
  23.     $select_tag .= '">';
  24.     $select_options = "";
  25.     foreach($options as $a) {
  26.       if(is_array($a)){
  27.         $ky = $a[0];
  28.         $val = $a[1];
  29.       }else{
  30.         $ky = $a;
  31.         $val = $a;
  32.       }
  33.       $select_options .= '<option value="' . $ky . '"';
  34.       if($selected==$ky){
  35.         $select_options .= ' selected="selected"';
  36.       }
  37.       $select_options .= '>' . $val . '</option>';
  38.     } 
  39.     $select_tag .= $select_options . '</select>';   
  40.     return $select_tag;
  41.   } 
  42. ?>

So now in view we can directly add in php tag

  1. <?php echo select_tag("select_name", array(), $selected_value, "classname", "onchange_javascript_function(this.value);"); ?>

In the project I am working on needed the onchange javascript function.
Its not mandatory. So you can just avoid or remove that from the code.

This code is available on github : php_common_helpers.git.

No comments:

Post a Comment