Posts Tagged ‘php’

Create a delimited list of SortedList Keys in C#

I love C#, but miss the simplicity of PHP sometimes.  Specifically when dealing with collections.  Recently I ran into a situation where PHP’s implode would have been perfect, but I wasn’t able to find any quick and easy built in solution.

I would like to be able to do this

string id_list = implode( ",", mySortedList.Keys );

I’m not aware of any built in ways to do this, so I wrote the following helper function.

///
/// Pass in a SortedList and this will return a string containing a delimited
/// list of keys separated by delim
///
///

///

/// key1{DELIM}key2{DELIM}keyN
public static string SortedListKeysToDelimList(SortedList sl, string delim)
{
StringBuilder sb_keys = new StringBuilder();

foreach (DictionaryEntry dl in sl)
{
sb_keys.Append( dl.Key.ToString() );

// append DELIM only if we're NOT on the last entry
if (dl.Key != sl.GetKey(sl.Keys.Count - 1))
{
sb_keys.Append( delim );
}
}

return sb_keys.ToString();
}

If there is any better way to do this, please leave me a comment.

PHP state picker options list maker

Here’s a static function I use in some of my PHP projects when I need to create a united states state picker.  It makes it really convenient to have an empty <select> in my form that wraps a $statelist variable containing my options.  The extra helpful part of this function is it can do selection for you if you pass in a state abbreviation.  Good for edit forms.

/**
 * Return an html options list full of states
 *
 * @param $selected_state string[optional]	abbreviation of state to select
 * @param $b_add_space bool[optional]		first option is blank
 * @return string
 */
public static function state_picker_options( $selected_state = "", $b_add_space=false )
{
	$states = array(
		'AL' => 'ALABAMA',
		'AK' => 'ALASKA',
		'AZ' => 'ARIZONA',
		'AR' => 'ARKANSAS',
		'CA' => 'CALIFORNIA',
		'CO' => 'COLORADO',
		'CT' => 'CONNECTICUT',
		'DE' => 'DELAWARE',
		'DC' => 'DISTRICT OF COLUMBIA',
		'FL' => 'FLORIDA',
		'GA' => 'GEORGIA',
		'HI' => 'HAWAII',
		'ID' => 'IDAHO',
		'IL' => 'ILLINOIS',
		'IN' => 'INDIANA',
		'IA' => 'IOWA',
		'KS' => 'KANSAS',
		'KY' => 'KENTUCKY',
		'LA' => 'LOUISIANA',
		'ME' => 'MAINE',
		'MD' => 'MARYLAND',
		'MA' => 'MASSACHUSETTS',
		'MI' => 'MICHIGAN',
		'MN' => 'MINNESOTA',
		'MS' => 'MISSISSIPPI',
		'MO' => 'MISSOURI',
		'MT' => 'MONTANA',
		'NE' => 'NEBRASKA',
		'NV' => 'NEVADA',
		'NH' => 'NEW HAMPSHIRE',
		'NJ' => 'NEW JERSEY',
		'NM' => 'NEW MEXICO',
		'NY' => 'NEW YORK',
		'NC' => 'NORTH CAROLINA',
		'ND' => 'NORTH DAKOTA',
		'OH' => 'OHIO',
		'OK' => 'OKLAHOMA',
		'OR' => 'OREGON',
		'PA' => 'PENNSYLVANIA',
		'PR' => 'PUERTO RICO',
		'RI' => 'RHODE ISLAND',
		'SC' => 'SOUTH CAROLINA',
		'SD' => 'SOUTH DAKOTA',
		'TN' => 'TENNESSEE',
		'TX' => 'TEXAS',
		'UT' => 'UTAH',
		'VT' => 'VERMONT',
		'VI' => 'VIRGIN ISLANDS',
		'VA' => 'VIRGINIA',
		'WA' => 'WASHINGTON',
		'WV' => 'WEST VIRGINIA',
		'WI' => 'WISCONSIN',
		'WY' => 'WYOMING'
	);

	$s_options_list = "";

	//add blank state?
	if($b_add_space)
	{
		$s_options_list .= "<option value=''></option>";
	}

	//construct list
	foreach( $states as $st => $name )
	{
		if( strtolower($st) == strtolower( $selected_state ) )
		{
			$s_options_list .= "<option value='$st' selected> $name </option>";
		}
		else
		{
			$s_options_list .= "<option value='$st'> $name </option>";
		}
	}

	return $s_options_list;
}