Wednesday, March 16, 2011


<script type="text/javascript">
window.onload=function() {
var sum = 0;

var dataTable = document.getElementById("table1");
//use querySelector to find all second table cells
var cells = document.querySelectorAll("td + td");

for (var i = 0; i < cells.length; i++) {
sum+=parseFloat(cells[i].firstChild.data);
}
//now add sum to end of table
var newRow = document.createElement("tr");

//first cell
var firstCell = document.createElement("td");
var firstCellText = document.createTextNode("Sum:");
firstCell.appendChild(firstCellText);
newRow.appendChild(firstCell);
//second row with sum
var secondCell = document.createElement("td");
var secondCellText = document.createTextNode(sum);
secondCell.appendChild(secondCellText);
newRow.appendChild(secondCell);


//add row to table
dataTable.appendChild(newRow);

}
</script>

Tuesday, March 15, 2011

Tuesday 3/15/11

I have been working on a lot of stuff but I have not had time to work much on tutorials lately.


<?php

//Recursion
// a recursive function is one that calls itself
function reverse_r($str) {
if (strlen($str)>0) {
reverse_r(substr($str, 1));
}
echo substr($str, 0, 1);
return;
}

function reverse_i($str) {
for ($i=1; $i echo substr($str, -$i, 1);
}
return;
}

reverse_r('Hello');

reverse_i('Hello');
?>

Friday, March 11, 2011

Friday, 3/11/11


<script type="text/javascript">
var euroToDollarRate = 1.21;
//try to conver the input into a number
var eurosToConvert = Number(prompt("How many Euros do you wish to convert?", ""));
//if the user hasn't entered a number, then NaN will be returned
if (isNaN(eurosToConvert)) {
//as the user to enter a value in numerals
document.write("Please enter the number in numerals");
//if NaN is not returned, then we can use the input
} else {
//and do the conversion as before
var dollars = eurosToConvert * euroToDollarRate;
document.write(eurosToConvert + " euros is " + dollars + " dollars");
}

</script>


Here is another tutorial


<script type="text/javascript">
//ask the user for a number and try to conver the input into a number
var userNumber = Number(prompt("Enter a number between one and ten.", ""));
//if the value of the userNumber is NaN, ask the user to try again
if (isNaN(userNumber)) {
document.write("Please ensure a valid number is entered");
//if the valeu is a number but over 10, as the user to try again
} else {
if(userNumber > 10 || userNumber < 1) {
document.write("The number you entered is not between 1 and 10");
//otherwise the numer is between 1 and 10 so
//write to the page
} else {
document.write("The number you entered was " + userNumber);
}
}
</script>


Here is another tutorial:



<script type="text/javascript">
//Store user entered number between 1 and 4 in userNumber
var userNumber = Number(prompt("Enter a number between 1 and 4"));
switch(userNumber) {
// If userNumber is 1, write out and carry on executing after case statement
case 1:
document.write("Number 1");
break;
case 2:
document.write("Number 2");
break;
case 3:
document.write("Number 3");
break;
case 4:
document.write("Number 4");
break;
default:
document.write("Please enter a numeric value between 1 and 4");
break;
}
</script>


Here is something I basically wrote myself. It's for adding rows/columns using javascript



<script type="text/javascript">
function insRow()
{
var newRow = document.getElementById('myTable').insertRow(0);
var newCell1 = newRow.insertCell(0);
newCell1.innerHTML = "34"
var newCell2 = newRow.insertCell(0);
newCell2.innerHTML = "76"
}
</script>

Thursday, March 10, 2011

Thursday 3/10/11


<script type="text/javascript">
var preInitArray = new Array("First Item", "Second Item", "Third Item");
document.write(preInitArray[0] + "
");
document.write(preInitArray[1] + "
");
document.write(preInitArray[2] + "
");
</script>


Here is another JavaScript tutorial. Use a string instead of an index number on an array!



<script type="text/javascript">
var anArray = new Array();
anArray[0] = "Fruit";
anArray["CostOfApple"] = 0.75;
document.write(anArray[0] + "
");
document.write(anArray["CostOfApple"]);
</script>


Here is another tutorial


<script type="text/javascript">
var anArray = new Array();
var itemIndex = 0;
var itemKeyword = "CostOfApple";

anArray[itemIndex] = "fruit";
anArray[itemKeyword] = 0.75;
document.write(anArray[itemIndex] + "
");
document.write(anArray[itemKeyword]);
</script>


Here is another tutorial



<script type="text/javascript">
var bannerImages = new Array();
bannerImages[0] = "banner1.png";
bannerImages[1] = "banner2.png";
bannerImages[2] = "banner3.png";
bannerImages[3] = "banner4.png";
var randomImageIndex = Math.round(Math.random()*2);
document.write("");
</script>


here is another tutorial - you can convert an array to a string using .join


<script type="text/javascript">
var arrayThree = new Array("John", "Paul", "George", "Ringo");
var lineUp = arrayThree.join(',');
alert(lineUp);
</script>


Here is another tutorial - split the string into an array


var longString = "Matthew, Mark, Luke, John";
var apostles = longString.split(',');
alert(apostles.length);


Here is a tutorial that I modified with a function to print the array



<script type = "text/javascript">
function printArray(array) {
document.write("Array: " + "

");
for (i=0; i<array.length; i++) {
document.write(array[i] + "
");
}
document.write("

");
}

var arrayToSort = new Array("Cabbage", "Lemon", "Apple", "Pear", "Banana");
printArray(arrayToSort);
var sortedArray = arrayToSort.sort();
printArray(sortedArray);
var reverseArray = sortedArray.reverse();
printArray(sortedArray);
</script>


Final tutorial for the night:


<script type = "text/javascript">
function compareValues(value1, value2) {
document.write("" + "Compare" + "");
document.write("

");
document.write(value1 + " = " + value2 + ": ");
document.write(value1 == value2);
document.write("
");
document.write(value1 + " > " + value2 + ": ");
document.write (value1 > value2);
document.write("

");
}

compareValues("Apples", "Oranges");

compareValues(18, 44);

</script>

Tuesday, March 8, 2011

Tuesday 3/8/11


<?php
//adds an element to the beginning of an array

$prices = array(5.95, 10.75, 11.25);
printf("

%s

\n", implode(',', $prices));

array_unshift($prices, 10.85);
printf("

%s

\n", implode(',', $prices));

array_unshift($prices, 3.35, 17.95);
printf("

%s

\n", implode(',', $prices));
?>


New



<?php
function array_insert(&$array, $offset, $new)
{
array_splice($array, $offset, 0, $new);
}
$languages = array('German', 'French', 'Spanish');
printf("
%s
\n", var_export($languages, TRUE));
array_insert($languages, 1, 'Russian');
printf("
%s
\n", var_export($languages, TRUE));
array_insert($languages, 3, 'Mandarin');
printf("
%s
\n", var_export($languages, TRUE));
array_insert($languages, 2, 'Korean');
printf("
%s
\n", var_export($languages, TRUE));

?>

Monday, March 7, 2011

Monday 3/7/11


<?php
function array_display($array, $pre=FALSE)
{
$tag = $pre ? 'pre' : 'p';
printf("<%s>%s\n", $tag, var_export($array, TRUE), $tag);
}
$arr1 = array(1, 2, 3);
$arr2 = array(10, 20, 30);
$arr3 = array(5, 10, 15, 20);

$comb1 = array_merge($arr1, $arr2);
$comb2 = array_merge($arr2, $arr1);
$comb3 = array_merge($arr3, $arr2, $arr1);

array_display($comb1);
array_display($comb2);
array_display($comb3);

$arr4 = array("a", "b", "c", "d");
$arr5 = array(7, 8, 9, 10);
echo "<hr>";
$comb4 = array_merge($arr4, $arr5);
array_display($comb4);
?>


Here is the next tutorial:



<?php
function array_eq_ident($arr1, $arr2)
{
printf("

The two arrays are %sequal.

\n", $arr1 == $arr2 ? '' : "not ");
printf("

The two are arrays are %sidentical.

\n", $arr1 === $arr2 ? '' : "not ");
}

$dogs = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'Alsatian', 'Snoopy' => 'Beagle');

$pups = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'Alsatian', 'Snoopy' => 'Beagle');

$mutts = array('Lassie' => 'Collie', 'Rin-Tin-Tin' => 'Alsatian', 'Bud' => 'Sheepdog', 'Snoopy' => 'Beagle');

print "

\$dogs and \$pups:

\n";
array_eq_ident($dogs, $pups);

print "

\$dogs and \$mutts:

\n";
array_eq_ident($dogs, $mutts);

?>

Sunday, March 6, 2011

Sunday 3/6/11

Just for fun I am going to put here the entire code (so far) for the process form php


<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$phoneNumber = $_POST['phoneNumber'];
$emailAddress = $_POST['emailAddress'];
$contactMethod = $_POST['contactMethod'];
$returnString = "requestContact.php";

$dataArray = array($firstName, $lastName, $phoneNumber, $emailAddress, $contactMethod);
$errorArray = array();


// This function will build a php url that will outline to the contactAdministration PHP form all the errors.
function buildErrorString($returnString, $dataArray, $errorArray) {
$returnString = $returnString . "?";
//use a foreach loop


for ($i=0; $i<count($dataArray); $i++) {
if ($dataArray[$i] != "") {
$name = "value" . $i;
$returnString = $returnString . $name . "=" . $dataArray[$i] . "&";
}
}

foreach ($errorArray as $value) {
$returnString = $returnString . $value . "=" . " Required" . "&";
}

return $returnString;
} //end function buildErrorString


// This function will deposit the data into the notepad file
function depositData($dataArray){
// getCwd() gets the current directory, useful for building the file stream pointer
$current_directory = getCwd();
$file_address = $current_directory . "/files/reqContact.txt";
$outputString = $dataArray[0] . "\t" . $dataArray[1] . "\t" . $dataArray[2] . "\t" . $dataArray[3] . "\t" . $dataArray[4] . "\n";
$fileOpen = fopen($file_address, 'a');
// frwrite() writes the contents of the outputString to the file stream pointed to by the $fileOpen handle
fwrite($fileOpen, $outputString, strlen($outputString));
fclose($fileOpen);


} //end function depositData

for ($i=0; $iif ($dataArray[$i] == "") {
$errorArray[] = "blank" . $i;
}
}



if (count($errorArray) > 0) {
$returnString = buildErrorString($returnString, $dataArray, $errorArray);
} else {
depositData($dataArray);
}

// strips off any extra ampersand off the end of the return string
$returnString = rtrim($returnString, "&");

header("location: $returnString");


?>

Friday, March 4, 2011

Friday 3/4/2011


<?php
$languages = array();
$languages[] = 'German';
$languages[] = 'French';
$languages[] = 'Spanish';
printf("

Languages: %s.

\n", implode(',', $languages));

array_push($languages, 'Mandarin', 'Japanese', 'Korean');
echo "
";
printf("

Languages: %s .

\n", implode(' / ', $languages));


?>

Thursday, March 3, 2011

http://www.killerphp.com/tutorials/object-oriented-php/

<?php
function array_list($array)
{
printf("

(%s)

\n", implode(', ', $array));
}

$arr1 = range(5,11); #integer start/end
array_list($arr1);

$arr2 = range(0, -5);
array_list($arr2);

$arr3 = range(3, 15, 3);
array_list($arr3);

array_List(range(20,0,-5));

array_List(range(2.4, 3.1, .1));

array_List(range('a', 'f'));
?>


Here is another tutorial:


$languages = array('German', 'French', 'Spanish');
printf("

Languages: %s .

\n", implode(', ', $languages));


Here is another tutorial:


$countries_languages = array('Germany' => 'German', 'France' => 'French', 'Spain' => 'Spanish');
echo "Array values: ";
printf("

Languages: %s.

\n",implode(',', array_values($countries_languages)) );
echo "Array keys: ";
printf("

Countries: %s.

\n",implode(',', array_keys($countries_languages)) );


Here is yet another tutorial:


$customers = array(array('first' => 'Bill', 'last' =>'Jones', 'age' => 24, 'state' => 'CA'),
array('first' => 'Mary', 'last' => 'Smith', 'age' => 32, 'state' => 'OH'),
array('first' => 'Joyce', 'last' => 'Johnson', 'age' => 21, 'state' => 'TX'),
);

printf("print_r():
%s
", print_r($customers, TRUE));
printf("var_export():
%s
", var_export($customers, TRUE));

print 'var_dump():
';
var_dump($customers);
print '
';

Wednesday, March 2, 2011

Wednesday 3.2.11


Partial Class Java2sPage
Inherits System.Web.UI.Page

Private Sub DisplayMessage()
Dim i As Integer
For i = 1 To 4
output.Text &= "Welcome to my website
"
Next i
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
output.Text = String.Empty
DisplayMessage()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
DisplayMessage()
End Sub
End Class


First C# script. Most of it was auto-generated.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("T");
}
}

Tuesday, March 1, 2011

Tuesday, March 1st 2011


<?php
function larger($x, $y) {
if ((!isset($x)) || (!isset($y))) {
return false;
} else if ($x >= $y){
return $x;
} else {
return $y;
}

}

$x = 6;
$y = 7;
$answer = larger($x, $y);
echo "X = $x";
echo "
";
echo "Y = $y";
echo "
";
echo $answer . " is the larger number ";

?>