Form Field Woes

I was working on something today… more of a proof of concept. I had a dynamic form from which the user would fill it out and click submit. They would then see if they filled it out correctly.

Well, I got a little stumped for a second or two regarding it… how would I check if the question was processed. Usually I do:

IsDefined("form.fieldname")

But with my question looking like:

answer_#q.question_id#

I couldn’t quite do that. CF kept telling me it hated me. (Honest, those were its exact words.)

So I had to figure something out. At first I thought I’d have to use Evaluate… but well would you know that would be the wrong course of action? Ran across a website that told me to do this:

form["answer_#q.question_id#"]

Would you know that it worked? Oh yes it sure did work. So that made my day.

Ant – Hello World

Usually you wouldn’t ever use a hello world example in Ant. Outputting text isn’t really the goal of the thing. But this will give you a chance to see how to run an ant build.

So, open up a text editor and save the following as build.xml.

<project name="HelloWorld">

  <target name="sayhello">
    <echo>Hello World!</echo>
  </target>
</project>

You would then run the program as:

ant sayhello

As you might expect, it simply says Hello World!

Echo messages are useful to track the process of an Ant Task when it’s going through specific procedures that you specify.

Ant For Multiple Deployments

I use Ant a lot when I program. Mostly for deploying whatever I’ve been coding to a test site. I currently have an ant build file that has two targets. One target will put the files to a tomcat server running ColdFusion 9, the other target will deploy the files to a server running Railo.

Eventually I’d like to have my build file run to all of the possible ColdFusion versions that I can. Now that would be something special.

I should start a section regarding Ant… now that would be something special.

Java Console – Input

Ah the fun times of inputting data from the command line in a Java program.

Java has come a long way in this respect. From starting with IO Streams to what they have now. Currently, there is a Console class in the java.io package. Here’s a simple test I did capturing data the user enters:

import java.io.Console;
import java.io.IOException;

public class Test {

	public Test() {

	}

	public static void main(String[] args) throws IOException {
		String in = null;

		Console c = System.console();
		if (c == null) {
			System.err.println("No console.");
			System.exit(1);
		}

		while (!(in = c.readLine("> ")).equals(".quit")) {
			// do something with the content you just captured
			System.out.println(in);
		}
	}
}

That’s a very simple program now isn’t it? Just accepts whatever you enter and echos it back out to the screen. If you type in .quit it will quit the program.

So yep, just a simple program for capturing data in a Console application.

Refreshing The Window Opener

So here’s a simple problem I had earlier this week while working on a personal project.

I had a webpage in an iframe that spanwed a window. The new window would either add a record to the database or update an existing record. The page that opened the window would then reflect the updated data.

Well for a while I tried various different things. Then I finally came upon the correct way to do it:

window.opener.location.reload(true);

So after I do my updates or additions, I run that code and it refreshes the data list and shows the new information.

Easy. As. Pie.

Using like in a cfqueryparam

So there I was… attempting to figure out the best way to to a like clause in a query statement using the cfqueryparam tag.

I had it coded like this:

like <cfparam cfsqltype="cf_sql_varchar" value="#arguments.search_criteria#">

Well that didn’t work. So I did some googling. After a little bit of searching, I finally found out how it should be coded.

like <cfparam cfsqltype="cf_sql_varchar" value="%#arguments.search_criteria#%">

After laughing at how simple that was… I used it. Worked like a charm.

MySQL DateTime

Been attempting to put a datetime variable into a MySQL Database via ColdFusion. For the life of me, I couldn’t figure out what I was doing wrong.

At first I tried:

#createODBCDateTime(now())#

For some reason that was only giving me the date and not the time.

Then I tried splitting up the date and time into two different fields, one for the date and the other for the time. Then I tried this code:

#createODBCDate(now())#

#createODBCTime(now())#

That gave me the date, but still not the time. Well I did some searching and then found there’s a built in function in MySQL to do this. Why not.

Talk about simple:

NOW()

Well guess what, it worked just like a charm.

Multidimensional Arrays

Ah Multidimensional Arrays. Nothing too crazy in JavaScript. Just a little bit of this and that.

So typical arrays are fun and easy. You create them like so:

var colors = new Array();
colors[0] = "Red";
colors[1] = "Yellow";
colors[2] = "Blue";

Well that’s fine and dandy, but what if you want to associate something with each of those? For example, what if you wanted to create a menu? You’ll need at minimum a link name and a target. We could do something like this:

var colors = new Array();
colors[0] = new Array();
colors[0][0] = "red.htm";
colors[0][1] = "Red";

colors[1] = new Array();
colors[1][0] = "yellow.htm";
colors[1][1] = "Yellow";

colors[2] = new Array();
colors[2][0] = "blue.htm";
colors[2][1] = "Blue";

function createColors() {
    document.write('<ul id="colors">');
    for (var i = 0; i < colors.length; i++) {
        var link = colors[i][0];
        var title = colors[i][1];
        var link_title = '<li><a href="' + link + '">' + title + "</a></li>";
        document.write(link_title);
    }
    document.write("</ul>");
}

Then we would call it like this:

<script type="text/javascript">
<!--
createColors();
//-->
</script>

Of course you could add more things, like an id, a title… whatever else you would need.

CSS Menu

Here’s a fun little css stylesheet that one can use for menus.

The CSS for the menu is:

#menu {
    padding:0px;
    width:120px;
}

#menu a {
    text-decoration:none;
    color:#000;
    display:block;
    border:1px solid #fff;
    padding-left:3px;
}

#menu a:hover {
    border:1px solid #000;
}

Which you can use with the following html:

<ul id="menu">
    <li><a href="#">Link One</a></li>
    <li><a href="#">Link Two</a></li>
    <li><a href="#">Link Three</a></li>
</ul>

This will producde a simple menu that when hovered over will display a black box around the menu item.

Of course you can change the background color, or the font etc. to whatever you need it to be. Ah the simplicity that is CSS.

The For Loop

There are two types of for loops in Java. Your traditional loop:

for (int i = 0; i < 10; i++) {

}

Or the advanced loop that will loop through items in say an array:

for (String s : stringArray) {

}

Let's go through both of these examples and see what comes of it.

First, let's create a String Array for the loops to process.

String[] strArray = {"one", "two", "three"};

Simple enough. Just an array containing three values of one, two and three.

With the traditional for loop, you would loop through the array like this:

for (int i = 0; i < strArray.length; i++) {
    System.out.println(strArray[i]);
}

As you might have guessed, it outputs the three values:


One
Two
Three

Now, let's do the same thing, but using the other for loop. Sometimes this loop is called a For Each loop.

for (String s : strArray) {
    System.out.println(s);
}

Again we get the three values:


One
Two
Three

I prefer the new method myself. It comes in handy. But there are times that you'll want to use the traditional for loop, (for example if you want to run through a Multidensional Array).

Happy Coding!