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

CornerBLUE, Inc. http://www.cornerblue.com

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

PHP: Insert Data Into MySQL Table Using An Array

Inserts the values of an array into a table. Also supports specifying if a specific field needs to be encoded using PASSWORD()
Parameters:
Table: Name of table to insert into
Data: array of $field->$value of new data
Password Field: Which field in the data array needs to be surrounded with PASSWORD() (optional)

   1  
   2  function mysql_insert_array($table, $data, $password_field = "") {
   3  	foreach ($data as $field=>$value) {
   4  		$fields[] = '`' . $field . '`';
   5  		
   6  		if ($field == $password_field) {
   7  			$values[] = "PASSWORD('" . mysql_real_escape_string($value) . "')";
   8  		} else {
   9  			$values[] = "'" . mysql_real_escape_string($value) . "'";
  10  		}
  11  	}
  12  	$field_list = join(',', $fields);
  13  	$value_list = join(', ', $values);
  14  	
  15  	$query = "INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")";
  16  	
  17  	return $query;
  18  }

C#: Break Long Strings of Text That Don't Contain A Space

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

   1  
   2  // Put this at the top
   3  using System.Text.RegularExpressions;
   4  //
   5  
   6  public string BreakLongString(string SubjectString, int CharsToBreakAfter)
   7  {
   8  	string Pattern = "\\S{" + CharsToBreakAfter + ",}";
   9  	int Counter = 0;
  10  	bool IsMatching = Regex.IsMatch(SubjectString, Pattern);
  11  	while (IsMatching)
  12  	{
  13  
  14  		Counter++;
  15  		string MatchedString = Regex.Match(SubjectString, Pattern).Value;
  16  		SubjectString = SubjectString.Replace(MatchedString.Substring(0, (CharsToBreakAfter - 1)), MatchedString.Substring(0, (CharsToBreakAfter - 1)) + " ");
  17  
  18  		// Prevent endless loops
  19  		if (Counter > 20) break;
  20  
  21  		// Check if we still have long strings
  22  		IsMatching = Regex.IsMatch(SubjectString, Pattern);
  23  	}
  24  
  25  	return SubjectString;
  26  }
  27  
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS