An array of C#, PHP, and HTML programming articles, tutorials, and resources

Archive for September, 2008

On a web page form, one method to accept user input is with a single line text field. This allows for a visitor the freedom to input anything. But what if you want to give the visitor a set of options to choose from in addition to the freedom of inputting anything? That is what is commonly known as a auto complete text field. As a visitor types into the auto complete text field, a list of selectable options (similar to a drop down list) will appear. This method is similar, but not the same as a Combo Box.

The best way to create an auto complete text field is with JavaScript. There are many various websites out on the internet that offer an solution. My personal favorite script to use is one created by zichun called Auto Complete Control. Of the many controls I tries, I like this for the following reasons:

  • It is free and use and download licensed under the Creative Common License
  • It is easy to implement only requiring adding two JavaScript scrips
  • Allows for many auto complete text field instances per page, each with many editable tweaks
  • The auto complete options quickly appear as the visitor types

The JavaScript files can be downloaded as a single zip file from their official website at http://www.codeproject.com/KB/scripting/jsactb.aspx. As of the writing of this tutorial, the two required files are common.js and actb.js. [More]

Calculating the Average from a List of numbers is accomplished through this easy function. The average is the sum of all values in a list divided by the total number of values. In this average example, we will assume the List consists of doubles (by means of a Generic List). For this to workOur Generic List is defined below:

List<double> myGenericDoubleList = new List<double>();
myGenericDoubleList.Add(5.2);
myGenericDoubleList.Add(8.4);
myGenericDoubleList.Add(435);
myGenericDoubleList.Add(76.9);
myGenericDoubleList.Add(3256.1);
myGenericDoubleList.Add(-85.2);

Now that we have defined our Generic Double List, we need to define our average function. Our function will take in a list of double values (the one we previously defined as myGenericDoubleList) and return a double value (which is our final double value). The average function reads in all double values and adds the value to a running sum. Finally, we divide the running sum by the total number of items in the list.

private double getAverage(List<double> doubleList)
{
    double total = 0;
    foreach (double value in doubleList)
    {
        total += value;
    }
    double average = total / doubleList.Count;
    return average;
}

Now that the function is ready to be used, we simply need to call the average function. An example is provided below:

double finalAverage = getAverage(myGenericDoubleList);

And there you have our complete example. Also important to know is a function has been provided out of the box with .NET Framework 3.5 that calculates the average of a list. To access this function, you be required to have a Generic List of double.

double finalDouble = myGenericDoubleList.Average()

PHP Foreach Loop

by Victor | September 12, 2008 in PHP | 1 Comment

There are many types of loops available in PHP. One of my favorite loops is the foreach loop. The foreach loop can loop through an entire array without the need to initialize an array counter. It is called a foreach loop because you get to define what you do to each loop element.

For the start of our example, we will create an array of strings that stores the number of page hits per page.M The naming convention I use for arrays is to begin the variable with ‘A_’. The array creation of ‘$A_hitsPerPage’ is shown below:

$A_hitsPerPage;
$A_hitsPerPage["index"] = "42";
$A_hitsPerPage["login"] = "12";
$A_hitsPerPage["logout"] = "13";
$A_hitsPerPage["news"] = "34";

Now that we have our array ‘$A_hitsPerPage’, we can access the various elements of the array. The syntax for the foreach loop is below:

foreach($A_hitsPerPage as $pageName => $hitCount) {
   echo "The page $pageName got $hitCount hits.<br />";
}

Though $A_hitsPerPage is required to be the array variable name, the other two variables ‘$pageName‘ and ‘$hitCount‘ can be a variable name of your choice. Just remember to replace the variable name from within the foreach curley brace accordingly. After execution of this PHP script, the page ouput looks is:

The page index got 42 hits.
The page login got 12 hits.
The page logout got 13 hits.
The page news got 34 hits.

Now you have a PHP foreach loop completely implemented with the output result. Important facts to remember about the foreach loop is that all elements will be evaluated. If you need more control, it may be helpful to consider a for loop.