<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Cornerblue's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 09:16:52 GMT</pubDate>
    <description>DZone Snippets: Cornerblue's Code Snippets</description>
    <item>
      <title>PHP: Create a SELECT input field</title>
      <link>http://snippets.dzone.com/posts/show/4555</link>
      <description>Creates a SELECT input field with an optional parameter to preselect an item&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function selectfield($optionsarray, $selected = "") {&lt;br /&gt;  $returnval = "";&lt;br /&gt;  foreach ($optionsarray as $field=&gt;$value) {&lt;br /&gt;    if ($field == $selected) {&lt;br /&gt;      $returnval .= "&lt;option selected value='" . $field . "'&gt;" . $value . "&lt;/option&gt;\n";&lt;br /&gt;    } else {&lt;br /&gt;      $returnval .= "&lt;option value='" . $field . "'&gt;" . $value . "&lt;/option&gt;\n";&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return $returnval;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Sep 2007 08:46:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4555</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>SQL SERVER: Delete Duplicate Rows with Primary Id</title>
      <link>http://snippets.dzone.com/posts/show/4482</link>
      <description>Deletes duplicates (leaving one instance) where the table has a primary key. Good for tables with Id, DupColumn, DupColumn...&lt;br /&gt;&lt;br /&gt;(This is MS-SQL specific)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DELETE&lt;br /&gt;FROM 	TableName&lt;br /&gt;WHERE 	Id NOT IN&lt;br /&gt;	(SELECT 	MAX(Id)&lt;br /&gt;        FROM   		TableName&lt;br /&gt;        GROUP BY 	DuplicateColumName1, DuplicateColumName2)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 31 Aug 2007 19:12:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4482</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>PHP: Insert Data Into MySQL Table Using An Array</title>
      <link>http://snippets.dzone.com/posts/show/4350</link>
      <description>Inserts the values of an array into a table. Also supports specifying if a specific field needs to be encoded using PASSWORD()&lt;br /&gt;Parameters:&lt;br /&gt;Table: Name of table to insert into&lt;br /&gt;Data: array of $field-&gt;$value of new data&lt;br /&gt;Password Field: Which field in the data array needs to be surrounded with PASSWORD() (optional)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function mysql_insert_array($table, $data, $password_field = "") {&lt;br /&gt;	foreach ($data as $field=&gt;$value) {&lt;br /&gt;		$fields[] = '`' . $field . '`';&lt;br /&gt;		&lt;br /&gt;		if ($field == $password_field) {&lt;br /&gt;			$values[] = "PASSWORD('" . mysql_real_escape_string($value) . "')";&lt;br /&gt;		} else {&lt;br /&gt;			$values[] = "'" . mysql_real_escape_string($value) . "'";&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	$field_list = join(',', $fields);&lt;br /&gt;	$value_list = join(', ', $values);&lt;br /&gt;	&lt;br /&gt;	$query = "INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")";&lt;br /&gt;	&lt;br /&gt;	return $query;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Jul 2007 22:59:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4350</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>PHP: Update MySQL Table Using An Array</title>
      <link>http://snippets.dzone.com/posts/show/4340</link>
      <description>Parameters:&lt;br /&gt;Table: Name of table to update&lt;br /&gt;Data: array of $field-&gt;$value with new values&lt;br /&gt;Id Field: Name of field to use as ID field&lt;br /&gt;Id Value: Value of ID field&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function mysql_update_array($table, $data, $id_field, $id_value) {&lt;br /&gt;	foreach ($data as $field=&gt;$value) {&lt;br /&gt;		$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));&lt;br /&gt;	}&lt;br /&gt;	$field_list = join(',', $fields);&lt;br /&gt;	&lt;br /&gt;	$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));&lt;br /&gt;	&lt;br /&gt;	return $query;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 23:25:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4340</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>PHP: Print List of States For A Select Field And Preselect A Value</title>
      <link>http://snippets.dzone.com/posts/show/4339</link>
      <description>Prints a list of US states for a &lt;select&gt; field and selects a predefined value. Use like this:&lt;br /&gt;echo "&lt;select name='billing_state'&gt;";&lt;br /&gt;state_list($_POST['billing_state']);&lt;br /&gt;echo "&lt;/select&gt;";&lt;br /&gt;&lt;br /&gt;You can also convert this function to return a value instead of directly outputting&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function state_list($sel='') {&lt;br /&gt;	echo "&lt;option value='AL'".($sel=='AL'?' selected':'')."&gt;Alabama&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='AK'".($sel=='AK'?' selected':'')."&gt;Alaska&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='AZ'".($sel=='AZ'?' selected':'')."&gt;Arizona&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='AR'".($sel=='AR'?' selected':'')."&gt;Arkansas&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='CA'".($sel=='CA'?' selected':'')."&gt;California&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='CO'".($sel=='CO'?' selected':'')."&gt;Colorado&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='CT'".($sel=='CT'?' selected':'')."&gt;Connecticut&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='DE'".($sel=='DE'?' selected':'')."&gt;Delaware&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='DC'".($sel=='DC'?' selected':'')."&gt;District of Columbia&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='FL'".($sel=='FL'?' selected':'')."&gt;Florida&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='GA'".($sel=='GA'?' selected':'')."&gt;Georgia&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='GU'".($sel=='GU'?' selected':'')."&gt;Guam&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='HI'".($sel=='HI'?' selected':'')."&gt;Hawaii&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='ID'".($sel=='ID'?' selected':'')."&gt;Idaho&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='IL'".($sel=='IL'?' selected':'')."&gt;Illinois&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='IN'".($sel=='IN'?' selected':'')."&gt;Indiana&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='IA'".($sel=='IA'?' selected':'')."&gt;Iowa&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='KS'".($sel=='KS'?' selected':'')."&gt;Kansas&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='KY'".($sel=='KY'?' selected':'')."&gt;Kentucky&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='LA'".($sel=='LA'?' selected':'')."&gt;Louisiana&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='ME'".($sel=='ME'?' selected':'')."&gt;Maine&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MD'".($sel=='MD'?' selected':'')."&gt;Maryland&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MA'".($sel=='MA'?' selected':'')."&gt;Massachusetts&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MI'".($sel=='MI'?' selected':'')."&gt;Michigan&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MN'".($sel=='MN'?' selected':'')."&gt;Minnesota&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MS'".($sel=='MS'?' selected':'')."&gt;Mississippi&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MO'".($sel=='MO'?' selected':'')."&gt;Missouri&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='MT'".($sel=='MT'?' selected':'')."&gt;Montana&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NE'".($sel=='NE'?' selected':'')."&gt;Nebraska&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NV'".($sel=='NV'?' selected':'')."&gt;Nevada&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NH'".($sel=='NH'?' selected':'')."&gt;New Hampshire&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NJ'".($sel=='NJ'?' selected':'')."&gt;New Jersey&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NM'".($sel=='NM'?' selected':'')."&gt;New Mexico&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NY'".($sel=='NY'?' selected':'')."&gt;New York&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='NC'".($sel=='NC'?' selected':'')."&gt;North Carolina&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='ND'".($sel=='ND'?' selected':'')."&gt;North Dakota&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='OH'".($sel=='OH'?' selected':'')."&gt;Ohio&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='OK'".($sel=='OK'?' selected':'')."&gt;Oklahoma&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='OR'".($sel=='OR'?' selected':'')."&gt;Oregon&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='PW'".($sel=='PW'?' selected':'')."&gt;Palau&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='PA'".($sel=='PA'?' selected':'')."&gt;Pennsylvania&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='PR'".($sel=='PR'?' selected':'')."&gt;Puerto Rico&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='RI'".($sel=='RI'?' selected':'')."&gt;Rhode Island&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='SC'".($sel=='SC'?' selected':'')."&gt;South Carolina&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='SD'".($sel=='SD'?' selected':'')."&gt;South Dakota&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='TN'".($sel=='TN'?' selected':'')."&gt;Tennessee&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='TX'".($sel=='TX'?' selected':'')."&gt;Texas&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='UT'".($sel=='UT'?' selected':'')."&gt;Utah&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='VT'".($sel=='VT'?' selected':'')."&gt;Vermont&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='VI'".($sel=='VI'?' selected':'')."&gt;Virgin Islands&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='VA'".($sel=='VA'?' selected':'')."&gt;Virginia&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='WA'".($sel=='WA'?' selected':'')."&gt;Washington&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='WV'".($sel=='WV'?' selected':'')."&gt;West Virginia&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='WI'".($sel=='WI'?' selected':'')."&gt;Wisconsin&lt;/option&gt;";&lt;br /&gt;	echo "&lt;option value='WY'".($sel=='WY'?' selected':'')."&gt;Wyoming&lt;/option&gt;";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 23:14:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4339</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>PHP: Email All Form Values</title>
      <link>http://snippets.dzone.com/posts/show/4338</link>
      <description>If you need a very basic contact form and don't want to write extra code for putting the value of each field into the email, you can use this basic email. It works best if you name your form fields something legible, such as First_Name&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if (isset($_POST['Submit'])) {&lt;br /&gt;	// Prepare message&lt;br /&gt;	$msg = "Time: " . date("m/d/y g:ia", time()) . "\n";&lt;br /&gt;	foreach ($_POST as $field=&gt;$value) {&lt;br /&gt;		if ($field != "submit") $msg .= $field . ": " . $value . "\n";&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if (mail("TOEMAIL", "SUBJECT", $msg, "From: FROM_NAME &lt;FROM@ADDRESS.com&gt;")) {&lt;br /&gt;		// Email was sent&lt;br /&gt;	} else {&lt;br /&gt;		// Erro sending email&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 23:09:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4338</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>PHP: Pass On Values For A Multi-Page Form</title>
      <link>http://snippets.dzone.com/posts/show/4337</link>
      <description>While the best way to do this involves saving the data into a database and passing an encrypted reference to the next page, this method will suffice for non-critical uses.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;foreach ($_POST as $field=&gt;$value) {&lt;br /&gt;	echo "&lt;input type=\"hidden\" name=\"" . $field . "\" value=\"" . $value . "\" /&gt;";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 23:03:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4337</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height</title>
      <link>http://snippets.dzone.com/posts/show/4336</link>
      <description>// This allows us to resize the image. It prevents skewed images and &lt;br /&gt;// also vertically long images caused by trying to maintain the aspect &lt;br /&gt;// ratio on images who's height is larger than their width&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)&lt;br /&gt;{&lt;br /&gt;	System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);&lt;br /&gt;&lt;br /&gt;	// Prevent using images internal thumbnail&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;&lt;br /&gt;	if (OnlyResizeIfWider)&lt;br /&gt;	{&lt;br /&gt;		if (FullsizeImage.Width &lt;= NewWidth)&lt;br /&gt;		{&lt;br /&gt;			NewWidth = FullsizeImage.Width;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;&lt;br /&gt;	if (NewHeight &gt; MaxHeight)&lt;br /&gt;	{&lt;br /&gt;		// Resize with height instead&lt;br /&gt;		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;&lt;br /&gt;		NewHeight = MaxHeight;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);&lt;br /&gt;&lt;br /&gt;	// Clear handle to original file so that we can overwrite it if necessary&lt;br /&gt;	FullsizeImage.Dispose();&lt;br /&gt;&lt;br /&gt;	// Save resized picture&lt;br /&gt;	NewImage.Save(NewFile);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:55:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4336</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>C#: Break Long Strings of Text That Don't Contain A Space</title>
      <link>http://snippets.dzone.com/posts/show/4335</link>
      <description>Long strings of text with no spaces breaks most tables since the browser doesn't know where to start a new line. This function breaks a string after a number of characters so that they can be properly displayed in tables&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Put this at the top&lt;br /&gt;using System.Text.RegularExpressions;&lt;br /&gt;//&lt;br /&gt;&lt;br /&gt;public string BreakLongString(string SubjectString, int CharsToBreakAfter)&lt;br /&gt;{&lt;br /&gt;	string Pattern = "\\S{" + CharsToBreakAfter + ",}";&lt;br /&gt;	int Counter = 0;&lt;br /&gt;	bool IsMatching = Regex.IsMatch(SubjectString, Pattern);&lt;br /&gt;	while (IsMatching)&lt;br /&gt;	{&lt;br /&gt;&lt;br /&gt;		Counter++;&lt;br /&gt;		string MatchedString = Regex.Match(SubjectString, Pattern).Value;&lt;br /&gt;		SubjectString = SubjectString.Replace(MatchedString.Substring(0, (CharsToBreakAfter - 1)), MatchedString.Substring(0, (CharsToBreakAfter - 1)) + " ");&lt;br /&gt;&lt;br /&gt;		// Prevent endless loops&lt;br /&gt;		if (Counter &gt; 20) break;&lt;br /&gt;&lt;br /&gt;		// Check if we still have long strings&lt;br /&gt;		IsMatching = Regex.IsMatch(SubjectString, Pattern);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return SubjectString;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:51:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4335</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>C#: Log A Message To A File</title>
      <link>http://snippets.dzone.com/posts/show/4334</link>
      <description>&lt;code&gt;&lt;br /&gt;// Put this at the top&lt;br /&gt;using System.IO;&lt;br /&gt;//&lt;br /&gt;&lt;br /&gt;public static void Log(string Message)&lt;br /&gt;{&lt;br /&gt;	File.AppendAllText(HttpContext.Current.Server.MapPath("~") + "/log.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": " + Message + Environment.NewLine);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:46:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4334</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
  </channel>
</rss>
