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

About this user

Lauri http://bytez.net

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Dropdown select list

   1  
   2  /**
   3   * generates html select dropdown list with options
   4   * if values is two dimensional then adds optgroup too
   5   * 
   6   * @param 	string	$name			selectbox name and id
   7   * @param 	array		$values		options
   8   * @param 	mixed		$selected	selected option
   9   * @param 	array		$attributes additonal attributes
  10   *
  11   * @return 	string	html source with selectbox
  12   */
  13  function html_selectbox($name, $values, $selected=NULL, $attributes=array())
  14  {
  15  	$attr_html = '';
  16  	if(is_array($attributes) && !empty($attributes))
  17  	{
  18  		foreach ($attributes as $k=>$v)
  19  		{
  20  			$attr_html .= ' '.$k.'="'.$v.'"';
  21  		}
  22  	}
  23  
  24  	$output = '<select name="'.$name.'" id="'.$name.'"'.$attr_html.'>'."\n";
  25  	if(is_array($values) && !empty($values))
  26  	{
  27  		foreach($values as $key=>$value)
  28  		{
  29  			if(is_array($value))
  30  			{
  31  				$output .= '<optgroup label="'.$key.'">'."\n";
  32  				foreach($value as $k=>$v)
  33  				{
  34  					$sel = $selected==$k ? ' selected="selected"' : '';
  35  					$output .= '<option value="'.$k.'"'.$sel.'>'.$v.'</option>'."\n";
  36  				}
  37  				$output .= '</optgroup>'."\n";
  38  			}
  39  			else
  40  			{
  41  				$sel = $selected==$key ? ' selected="selected"' : '';
  42  				$output .= '<option value="'.$key.'"'.$sel.'>'.$value.'</option>'."\n";
  43  			}
  44  		}
  45  	}
  46  	$output .= "</select>\n";
  47  
  48  	return $output;
  49  }
  50  


   1  
   2  $values = array('animals'=>array(
   3  			1=>'dog', 'cat', 'horse'
   4  		),
   5  		'birds'=>array(
   6  			4=>'gull', 'eagle', 'sparrow')
   7  		);
   8  
   9  $selected = isset($_REQUEST['select']) ? $_REQUEST['select'] : 4; // selected item
  10  
  11  echo html_selectbox('select', $values, $selected, array('size'=>10));
  12  

get_os_

   1  
   2  function get_os_($user_agent)
   3  {
   4  	$oses = array (
   5  		'Windows 3.11' => 'Win16',
   6  		'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
   7  		'Windows 98' => '(Windows 98)|(Win98)',
   8  		'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
   9  		'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
  10  		'Windows 2003' => '(Windows NT 5.2)',
  11  		'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
  12  		'Windows ME' => 'Windows ME',
  13  		'Open BSD'=>'OpenBSD',
  14  		'Sun OS'=>'SunOS',
  15  		'Linux'=>'(Linux)|(X11)',
  16  		'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
  17  		'QNX'=>'QNX',
  18  		'BeOS'=>'BeOS',
  19  		'OS/2'=>'OS/2',
  20  		'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)'
  21  	);
  22  
  23  	foreach($oses as $os=>$pattern)
  24  	{
  25  		if (eregi($pattern, $user_agent))
  26  			return $os;
  27  	}
  28  	return 'Unknown';
  29  }


For using:
   1  
   2  print get_os_($_SERVER['HTTP_USER_AGENT']);

get_browser_

This function determines visitor browser.

   1  
   2  function get_browser_($user_agent)
   3  {
   4  	$browsers = array(
   5  		'Opera' => 'Opera',
   6  		'Mozilla Firefox'=> '(Firebird)|(Firefox)',
   7  		'Galeon' => 'Galeon',
   8  		'Mozilla'=>'Gecko',
   9  		'MyIE'=>'MyIE',
  10  		'Lynx' => 'Lynx',
  11  		'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
  12  		'Konqueror'=>'Konqueror',
  13  		'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)',
  14  		'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
  15  		'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
  16  		'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
  17  	);
  18  
  19  	foreach($browsers as $browser=>$pattern)
  20  	{
  21  		if (eregi($pattern, $user_agent))
  22  			return $browser;
  23  	}
  24  	return 'Unknown';
  25  }


For using:
   1  echo get_browser_($_SERVER['HTTP_USER_AGENT']);
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS