Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

PHP Select Form Helper (See related posts)

// takes an array of values and a value to match, and outputs formatted <option>s with the <option> matching $match selected
// must be manually wrapped in <select></select to allow for maximum flexibility

function selectHelper($values, $match)
{
  $keys = array_keys($values);
  $i = 0;
	
  foreach($values as $option)
  {
    $selected = null;
		
    if($match == $keys[$i])
      $selected = " selected";
			
    echo "	<option value=\"".$keys[$i]."\"$selected>".$option."</option>\n";
		
    $i++;
  }
}

//sample usage:
$values = array(
  "lb" => "Pounds",
  "ea" => "Each",
  "oz" => "Ounces");
				
  selectHelper($values, $product->unit);

You need to create an account or log in to post comments to this site.


Click here to browse all 4881 code snippets

Related Posts