novicecoder.com

August 20, 2006

Find and replace in SQL tables

Pretty obvious, but I had to look it up so I figured I should save it here. Say I misspelled “people” as “peeple” and I wanted to fix them all in my database. Just do:

UPDATE table SET field=REPLACE(field, ‘peeple‘, ‘people‘ );
Filed under: Site Info — Kevin @ 6:17 am

July 20, 2006

Implode checkbox group arrays into query strings

In going from an html form to php code and finally to a sql query there are a lot of steps that could get messy, but fortunately php has a simple way to do this all. Let’s say I have some checkbox options on my html form for a user to select the sports they like.

<input type="checkbox" name="sports[]" value="baseball">
<input type="checkbox" name="sports[]" value="basketball">
<input type="checkbox" name="sports[]" value="football">
<input type="checkbox" name="sports[]" value="hockey">

Note how the checkboxes are grouped by giving them the same name with the index ([]) suffix. This makes it so that an array containing the checked values is returned. The next step is to use the php function implode. This function takes an array and creates a delimited list of the array items connected by whatever glue string you want. The simple example is to generate a comma delimited list like “basketball, baseball, football, hockey”.

$csv = implode(",", $_POST['sports']);

In this example we want to build a query string.

$query = "SELECT * FROM sportstable WHERE
         sport='" . implode("' OR sport='",$_POST['sports']) . "'";

This would create the string, “SELECT * FROM sportstable WHERE sport=’basketball’ OR sport=’baseball’ OR sport=’football’”, with the items that were checked by the user. Of course you’ll want to validate the input first (check that the array count is greater than zero, sql injection, etc.) but user input validation is a whole post of its own.

Filed under: PHP, Site Info, Strings, Arrays — Kevin @ 5:42 am

June 28, 2006

Visual Studio margin guide lines

Some of us like to keep our lines under 80 characters wide (and some are forced to by coding standards). Either way, it’s a little bit easier to keep track of it with a guide line denoting the margins. Just modify the registry and create a string value at “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.1\Text Editor” (or change the 7.1 to 8.0 if you’re using Visual Studio 2005). Name the value “Guides” and set it to “RGB(204,204,204) 79″ where the (204,204,204) is the color you want and the 79 is the zero based column you want the guide on. You can also add multiple guides by adding a comma delimited list like, “RGB(204,204,204) 79,99″ to get guides at the 80 and 100 character mark.

For ease, here are some .reg files that will produce a grey line at the 80 column mark:
Visual Studio 2003
Visual Studio 2005

Filed under: IDEs, Visual Studio — Kevin @ 3:32 am

June 11, 2006

Regular expressions

Regular expressions (regex) are used in many situations. Web server administration and search algorithms are just a couple of the tasks where I’ve found myself looking up references on them. Rather than always searching for the latest search engine optimized page on the internet, I figure I should just keep it here…

Syntax Definition Usage
. Any character /l.mb/ - Will match “lamb” or “limb” (or any other l_mb)
? Matches preceding character (or group) zero or one time /Brand?/ - Matches both “Bran” and “Brand”
+ Matches preceding character (or group) one or more times /Class A+/ - Matches “Class A”, “Class AA”, “Class AAA”, and so on
* Matches preceding character (or group) any number of times /abcd*/ - Matches “abc”, “abcd”, “abcdd”, “abcddd”, and so on
{n} Matches preceding character (or group) n number of times /Mag{2}et{2}e/ - Matches “Maggette”
{n,m} Matches preceding character (or group) between n and m number of times inclusive /Clip{1,3}ers/ - Matches “Clipers”, “Clippers”, and “Clipppers”
{n,} Matches preceding character (or group) n or more times /Clip{2,}ers/ - Matches “Clippers”, “Clipppers”, “Clippppers”, and so on
^ Beginning of the string/line /^begin/ - Find strings/lines where the first characters are “begin”
$ End of the string/line /end$/ - Find strings/lines where the last characters are “end”
[abcd] Any of the characters /sa[dmw]/ - Matches “sad”, “sam”, and “saw”
[^abcd] Not one of the characters /sa[^dm]/ - Matches anything that contains “sa” followed by another character except for “sad” and “sam”
[a-d] Any character in the range /[a-d]/ - Matches a, b, c, or d. Can use numbers too like /[1-5]/ to match 1, 2, 3, 4, or 5
(abcd) Grouping /(Elton)+/ - Matches one or more occurances of “Elton”
(a|b) a or b /(Chris|Shaun)/ - Matches either “Chris” or “Shaun”
!(a) Not in this group /!(Donald)/ - Doesn’t contain “Donald”
*?, +?, ??, {n}? Least number of possible matches /bo+?/ - Given the string, “boo”, would return “bo”
/bo+/ - Would return “boo” in this case
Filed under: General, Algorithms — Kevin @ 10:49 pm

June 7, 2006

Better array pushing

In any language, you’ll often find yourself working with arrays. PHP offers a couple methods for adding items to them. Say you have an array:

$arrayvals = array("elton", "sam");

and you want to add the value, “shaun” to it. You can either do:

$arrayvals[] = "shaun";

or you can do:

array_push($arrayvals, "shaun");

So which is better, or does it even matter? The answer is the first one, $arrayvals[]=”shaun”;, because according to the php manual, there is no overhead of calling a function with this method. A minor difference, but when writing web apps, every line you write is potentially called millions of times per second so you don’t want to be wasting cycles for no reason. Is there a point to array_push then? Remember that array_push allows adding multiple values at once so it’s easier to do:

array_push($arrayvals, "shaun", "corey", "chris");
Filed under: PHP, Arrays — Kevin @ 2:32 am

June 6, 2006

String testing made easy

Do you have lots of places in your code where you do:

if (stringval == null || stringval.Length == 0)
{
  handle string...
}

Well, .NET 2.0 adds a static method to the string class for IsNullOrEmpty. So now you can just test against string.IsNullOrEmpty(stringval) and save yourself a few keystrokes.

Filed under: C#, Strings — Kevin @ 1:42 am

June 2, 2006

Purpose

This is my log for random coding things that make life easier. Most things won’t be too technically advanced, but rather buried features that might not be obvious. They’re typically things I’ve learned by writing some long method and having someone else come along and ask why I didn’t just do it this other simpler way.

Filed under: Site Info — Kevin @ 7:00 pm

Home
© 2006 NoviceCoder | All rights reserved.