Friday, January 21, 2011

Friday 1/21/11


<?php
echo fix_names("William", "henry", "gatES");

function fix_names($n1, $n2, $n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
return $n1 . " " . $n2 . " " . $n3;
}
?>


Here is more PHP code from the book by Robin Nixon "Learning PHP, MySQL, and JavaScript"


<?php
$names = fix_names("WILLIAM", "henry", "gatES");
echo $names[0] . " " . $names[1] . " " . $names[2];

function fix_names($n1, $n2, $n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
return array($n1, $n2, $n3);
}


?>


Here is another exercise from the Robin Nixon book


<?php
echo "Returning values from a function by reference";
echo "
";

$a1 = "William";
$a2 = "henry";
$a3 = "gatES";

echo $a1 . " " . $a2 . " " . $a3 . "
";
fix_names($a1, $a2, $a3);
echo $a1 . " " . $a2 . " " . $a3;

function fix_names(&$n1, &$n2, &$n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
}

?>


Here is 5-9


<?php

if (function_exists("array_combine"))
{
echo "Function exists";
}
else
{
echo "Function does not exist - better write our own";
}

?>


Example 5-11



<?php
$object = new User;
print_r($object); echo"<br />";
$object->name = "Joe";
$object->password = "mypass";
print_r($object);
echo "
";

$object->save_user();

class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}
?>



Here is another quick tutorial


<?php
$object1 = new User();
$object1->name = "Alice";
$object2 = $object1;
$object2->name = "Amy";
echo "object1 name = " . $object1->name . "
";
echo "object2 name = " . $object2->name;

class User
{
public $name;
}
?>


Here is another tutorial by Robin Nixon


<html>

<body>
<?php
echo "Inhereting and extending a class";
echo "

";
$object = new Subscriber;
$object->name = "Fred";
$object->password = "pword";
$object->phone = "012 345 6789";
$object->email = "fred@bloggs.com";

$object->display();


class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}

class Subscriber extends User
{
public $phone, $email;

function display()
{
echo "Name: " . $this->name . "
";
echo "Pass: " . $this->password . "
";
echo "Phone: " . $this->phone . "
";
echo "Email: " . $this->email;
}

}

?>
</body>
</html>


Here is the final PHP tutorial for the day


<??php
$object = new Son;
$object->test();
$object->test2();

class Dad
{
function test()
{
echo "[Class Dad] I am your Father
";
}
}

class Son extends Dad
{
function test()
{
echo "[Class Son] I am luke
";
}

function test2()
{
parent::test();
}
}

?>


Here is a JavaScript tutorial from the Shelley Powers book. This is not all of it, I am not posting the HTML and CSS as it is not really relevant.


<script type="text/javascript">
//<![CDATA]

window.onload=function() {
document.getElementById("searchSubmit").onclick=doSearch;
}

function doSearch()

//only problem is that it is case-dependent.
{
// get pattern
var pattern = document.getElementById("pattern").value;
var regExpression = new RegExp(pattern, "g");


//get string
var searchString = document.getElementById("incoming").value;
var matchArray;
var resultString = "
";
var first=0; var last=0;

//find each match
while ((matchArray = regExpression.exec(searchString)) != null) {
last = matchArray.index;
// get all of string up to match, concatenate
resultString += searchString.substring(first, last);

// add matched, with class
resultString += "" + matchArray[0] + "";
first = regExpression.lastIndex;
}

// finish off string
resultString += searchString.substring(first, searchString.length);
resultString += "
";

// insert into page
document.getElementById("searchResult").innerHTML = resultString;
}



</script>


I have to admit, I got all of it typed in, got it to work, renamed a few of the variables even (I don't like short variable names) but...still not completely at ease with it, regular expressions are a bit complicated, I think.

Here is the code from Lab 4 of my JavaScript class




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>



<!-- I decided not to embed the style sheet but rather link to it -->
<link rel="stylesheet" type="text/css" href="temperatureStyles.css" media="screen" />
<script type="text/javascript">
var convertToCelsius = false;
var convertToFaranheit = false;

// This function sets the conversion to Farenheit as well as creates a subtle visual effect by turning
// the Farenheit text next to the radio button blue
function blueF(){
document.getElementById("spanF").className = "blueLine";
document.getElementById("spanC").className = "blackLine";
convertToFahrenheit = true;
convertToCelsius = false;
}

//note to self: Fahrenheit spelled F a h r e n h e i t
function blueC(){
document.getElementById("spanC").className = "blueLine";
document.getElementById("spanF").className = "blackLine";
convertToFahrenheit = false;
convertToCelsius = true;
}


// This function checks to see that a radio button has been selected, that the text field is not blank, and that the text blank
// contains a number
function validateEntry(value){

if (convertToCelsius == false && convertToFahrenheit == false)
{
alert("Please select Faranheit or Celsius");
return false;
}
else if (value == "")
{
alert("Please enter a value");
return false;
}
else if (isNaN(value) == true)
{
alert("Please enter a numeric value");
return false;
}
else
{
return true;
}
}


// This function will branch into two different options using a conditional operator (provided that the data entered is valid)
function convertIt(){


var entryData = document.getElementById("temp").value;

// use a seperate function to validate the data
if (validateEntry(entryData))
{
document.getElementById("temp").value = "";
// I finally dared to write a conditional operator
convertToCelsius==true ? convertFromFahrenheit(entryData) : convertFromCelsius(entryData);
}
else
{
document.getElementById('results').innerHTML = "Please make sure your entry is correct.";
}

}


function convertFromCelsius(entryData) {
var conversionData = entryData
conversionData = ((entryData * 1.8) + 32);
document.getElementById('results').innerHTML = "The temperature is " + conversionData + " degrees Fahrenheit";
document.getElementById('results').innerHTML += "<P></P>The temperature you converted <i>from</i> was " + entryData + " degrees Celsius";
}

function convertFromFahrenheit(entryData)
{
var conversionData = entryData
conversionData = ((entryData - 32) * .55);
document.getElementById('results').innerHTML = "The temperature is " + conversionData + " degrees Celsius";
document.getElementById('results').innerHTML += "

The temperature you converted from was " + entryData + " degrees Fahrenheit";
}





</script>

</head>
<body>

<!-- This DIV will hold the site in the center of the screen. -->
<div id="main">


<!-- This div will float left and contain the textboxes for entering the data -->
<!-- We are about to learn about jQuery and rounded corners for our advanced XHTML class -->
<!-- but I figured I better not mess around with that for now -->

<div id="left">
<h1 class = "header">Enter Temperature Here</h1>
<form id="entryForm">
Temperature: <input type="text" id="temp"></input>
<BR>
Convert to:
<br>
<span id="spanF">Fahrenheit </span><input type="radio" name="rSelect" id="radio-1" value="1" onclick="blueF()"></input>

<span id="spanC">Celsius </span><input type="radio" name="rSelect" id="radio-2" value="1" onclick="blueC()"></input>
</form>
<input type="button" id="btnInput" value="Convert" onclick="convertIt()"></input>
</div>



<!-- This div will float right and present the output for data -->
<div id="right">
<h1 class = "header">View Converted Temperature Here</h1>
<div id="results">

</div>
</div>



</div>
</body>
</html>

No comments: