AndyD.Org - Coding Tips, Tricks, and Tutorials


Difference Between ID and Name Attributes in HTML?
in HTML 5 on 06/30/12 02:42PM

What's the difference between those ID and Name attributes used on almost every HTML control any way?  They both seem to naming the control, and they are usually named the same thing in most code examples.

  • ID: used for the Document Object Model (DOM) of the page.  Used mostly for JavaScript. Should be unique IDs for each control on the page.  For example, each radio button should have a different ID specified.
  • Name: used when posting back on a form.  Doesn't have to be unique.  For example, all the radio buttons that answer one question like 'What color?' can all have the same name of 'color' with each radio button may have a difference ID ('blue','red','green', etc.).

Uploading Files With PHP
in PHP on 06/30/12 02:37PM

The first thing you must do to enable file uploading with PHP is to create an HTML input control, then provide a button for the user to actually upload the file.  The HTML should look something like this:


<html lang="en">
<meta charset="UTF-8">
<title>Upload Example</title>


<form action="" method="post" enctype="multipart/form-data" id="uploadFile">
          <label for="fileupload">Upload File:</label>
          <input type="file" name="fileupload" id="fileupload">
          <input type="submit" name="uploadbutton" id="uploadbutton" value="Upload File">




When uploading a file with PHP, the file is first copied to a temporary folder on the server.  If this file isn't immediately saved, the file will disappear.  Insert the following code above the <!DOCTYPE HTML> block:

//set the maximum upload size in bytes
$max = 51200;

if (isset($_POST['uploadbutton'])) {
     //define the path to the upload folder
     $destination = '/path/upload_testfolder/';
     //move the file to the upload folder and rename it
     move_uploaded_file($_FILES['fileupload']['tmp_name'], $destination . $_FILES['image']['name']);

How does this code work?  It's actually simple.  We first create the $destination variable (purple) that holds of the value of our path of where to save the file.  We then call the PHP function move_uploaded_file that takes two parameters: the path of the temporary file (green) and the path of the file's final upload location (blue).  The $_FILES super variable gives us the 'tmp_name' which is the full path of the temporary file (that won't get saved until we give it the destination!).  We take the file and copy it from here into our destination folder.  When saving to our destination file, we get the 'name' of the original file from the $_FILES variable and store that file in our destination folder.

How To Write Methods With C#
in C# on 06/22/12 12:53AM

Method allows you to break your code into small chunks.  Rather than putting all your code in a big, long Main() method, the Main() method should be used as a "controller" that calls other methods.  You can pass parameters to a method, have it perform some code, then return a value back.  For example:

public static void Main()
   string a;

   a = MultiplyBy3(5);


int MultipyBy3(int num)
   return num * 3;

In the above example, MultiplyBy3 is your method.  It accepts an integer (num) as the input parameter, and will return an integer back. 

The string variable, a, gets assigned to this return value then displayed in the console.  It passes a 5 as the input parameter to the MultiplyBy3 method.  The method takes the 5, multiples by 3, then returns the result which gets assigned to the a variable.  The resulting output is 15.

Note: If the method doesn't return a value, but rather just performs some code, then you can use the static reserved word instead of a return data type.



Formatting Text In HTML5
in HTML 5 on 06/19/12 11:31PM

HTML5 gives us three markup tags that we can use to format body text:

  • <small></small> - Used to make text slightly smaller than the surrounding text.
  • <del></del> - Used to show a strikethough, or text that should be deleted.
  • <ins></ins> - Used to show text thats has been "inserted" into the surrounding text.  Shows with an underline.

Here are examples of each.  See if you can spot them!


This text is supposed to be a little smaller than the surrounding text.  Please delete this wordThis word should be added to your paragraph.

Conditional Statements In PHP
in PHP on 06/19/12 11:20PM

IF statements in PHP are used to make decisions, and are very simple as in most programming languages.  A basic conditional statement follows this pattern:

if ($a == $b)


//code here to handle when $a equals $b


Notice that the two equal signs (==) are used to test for equality (to check if the value stored in $a matches the value stored in $b).  A single equal sign (=) is used for assigning a value to a variable.  Also, you'll notice that the line of code (in this case a comment!) doesn't need a semicolon.  In PHP, anything inside the curly braces {} is treated as one unit of code that will get executed if the condition ($a equals $b) is true.  If $a doesn't equal $b, the code inside of the curly braces will not get executed.

What about code for when the condition is not true?  You can do this by using an Else statement:

if ($a == $b)


//code here to handle when $a equals $b

} else {

//code here to handle when $a DOES NOT equal $b