Code Samples


PHP Code Samples

I have been coding with PHP for 3 years now and I really enjoy learning more and more about what I can do with this language.

//If there is an url variable name page that is set,
//then determine what it is and go to the appropriate page.
if(isset($_REQUEST['page'])) {
	switch($_REQUEST['page']) {
		case php:
			include("includes/php.inc");
			break;
			
		case javascript:
			include("includes/javascript.inc");
			break;
						
		case mysql:
			include("includes/mysql.inc");
			break;
							
		case ajax:
			include("includes/ajax.inc");
			break;
					
		case voodoo:
			include("includes/voodoo.inc");
			break;
							
		case rofe:
			include("includes/rofe.inc");
			break;
							
		case oasis:
			include("includes/oasis.inc");
			break;

		case resume:
			include("includes/resume.inc");
			break;
							
		case thankyou:
			include("includes/thankyou.inc");
			break;
														
		//this option, in theory, should actually never be hit but
		//I put it in here for that just in case scenario.
		default:
			include("includes/default.inc");
			break;
	} //end switch statement
} else {
	include("includes/default.inc");
} //end if(isset($_REQUEST['page']))
												

This sample is actually part of the old index page of this site. I use a simple switch statement to determine what should be loaded into what was the main content area via an include statement.

 
for($i = 0;$i < $numrows2;$i++) {
  	if($entity_id[$i] == $_GET['eid']) {
  	  	echo "";
  	} else {
	  	echo "";
	}
}
												

In this code sample I am using PHP to dynamically load an html drop down select statement. This particular code is actually part of an AJAX function I had to set up for my employer. They wanted it so that when a different option in the select box was chosen, various other fields on the screen would automatically update with the appropriate values without reloading the whole page again.

 

MySQL Code Samples

I have done a lot of PHP and MySQL work. One of the first things I ever did as a freelance web developer was to create a system to capture, store and display user testimonials. Below is the standard query used for the retrieval of the testimonials that have been stored in the MySQL database.


$sql = "select * from rf_testimonials order by tstmnl_order desc, tstmnl_date desc";
$result = @mysql_query($sql, $db[connection]) or die("Couldn't execute query.
$sql
".mysql_error()); $num = 0; while ($row = mysql_fetch_array($result)) { // if the testimonial has been approved, display it if($row[6] == 1) if($review_year >= $yester_year) { $num = $num + 1; $tstmnl_id = $row[0]; $tstmnl_date = $row[1]; $tstmnl_name = stripslashes($row[2]); $tstmnl_email = stripslashes($row[3]); $tstmnl_phone = stripslashes($row[4]); $tstmnl_review = stripslashes($row[5]); $tstmnl_type = stripslashes($row[7]); $tstmnl_file = stripslashes($row[8]); $tstmnl_location = stripslashes($row[9]);

This is only a small part of a testimonials system I put together for a company I do freelance work for called BlodgetTech. This system was built for dentist and doctor sites and allows a user to enter their own review of the dentist or doctor. There is also another part to the system where an admin person for the website can enter user reviews that were sent in to them via snail mail or voice reviews that were recorded through another site. If the review was sent via snail mail anyone looking at the testimonials page and reading the testimonial can also click on a thumbnail of the scanned in image of the review. If the review was a voice recording there are play and pause buttons next to the review so that one can listen to the review instead of simply reading it.

 
if(!$action == 1 && !$action == 2 && !$action == 3)
{
	$sql = "select * from rf_testimonials where ";
	$sql .= "tstmnl_name = '".$name."' ";
	$sql .= "and tstmnl_review = '".$testimonial."' ";
	$sql .= "and tstmnl_status = 1";

	$result = mysql_query($sql, $db[connection]) or die("Couldn't execute select query.
$sql
".mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { // if the testimonial already exists, then mark it for deletion. $sql = "update rf_testimonials "; $sql .= "set tstmnl_status = -1 "; $sql .= "where tstmnl_name = '".$name."' "; $sql .= "and tstmnl_review = '".$testimonial."' "; $sql .= "and tstmnl_status = 1"; $result = @mysql_query($sql, $db[connection]) or die("Couldn't execute update query.
$sql
".mysql_error()); } } // end if !$action == 1 ... // if $action is 1 then we approve only // if $action is 2 then we update and approve // if $action is 3 then we delete if($action == 1) // means approve { $sql = "update rf_testimonials set "; $sql .= "tstmnl_status = 1 "; $sql .= "where tstmnl_id = $reviewid"; } else if($action == 2) // means update and approve { $sql = "update rf_testimonials set "; //$sql .= "tstmnl_date = $now, "; $sql .= "tstmnl_name = '".$name."', "; $sql .= "tstmnl_email = '".$email."', "; $sql .= "tstmnl_phone = '".$phone."', "; $sql .= "tstmnl_review = '".$testimonial."', "; $sql .= "tstmnl_status = 1, "; $sql .= "tstmnl_type = $type,"; $sql .= "tstmnl_file = '".$media."', "; $sql .= "tstmnl_location = '".$location."' "; $sql .= "where tstmnl_id = $reviewid"; } else if($action == 3) // means delete { $sql = "update rf_testimonials set "; $sql .= "tstmnl_status = -1 "; // -1 is the status for delete $sql .= "where tstmnl_id = $reviewid"; } else if($action == 4) // this is a new media review that was entered by the admin { $sql = "insert into rf_testimonials (tstmnl_id, tstmnl_date, tstmnl_name, tstmnl_email, tstmnl_phone, tstmnl_review, tstmnl_status, tstmnl_type, tstmnl_file, tstmnl_location) values (NULL, \"$now\", \"$name\", \"$email\", \"$phone\",\"$testimonial\",1,$type,\"$media\",\"$location\")"; } else // there was no $action variable set { // the tstmnl_status field indicates wether the review is not approved (0), approved (1), or deleted (-1) $sql = "insert into rf_testimonials (tstmnl_id, tstmnl_date, tstmnl_name, tstmnl_email, tstmnl_phone, tstmnl_review, tstmnl_status, tstmnl_type, tstmnl_file, tstmnl_location) values (NULL, \"$now\", \"$name\", \"$email\", \"$phone\", \"$testimonial\", 0, 0, NULL, \"$location\")"; } $result = @mysql_query($sql, $db[connection]) or die("Couldn't execute query.
$sql
".mysql_error());

This section of code is setting up various sql statements depending upon an action variable.

 

CSS Code Samples

I can do both an HTML table based layout as well as a CSS based tableless layout. I really like CSS a lot and continue to learn new ways to style objects.

/* Stylesheet for SpinTech Web Development website */

/* default styles for the whole site */
html, body {
	margin: 0;
  	padding: 0;
  	width: 100%;
    height: 100%;
	min-height: 100%;
	font-size : .9em;
    background-color: #87b7f3;
	color : #073670;
    font-family : Verdana, Arial, sans-serif;
}
h1 {
	font-size: 1.3em;
	font-weight: bold;
}
h2 {
  	font-size: 1.2em;
	font-weight: bold;
}
h3 {
  	margin: 0;
  	padding: 0;
	font-size: 1.1em;
  	font-weight: normal;
  	display: block;
}
h4 {
	font-size: 1em;
	font-weight: bold;
	text-decoration: underline;
}
.middle {	/* simple class for center text */
	text-align: center;
}
.strong {	/* simple class for making text bold */
	font-weight: bold;
}
p {
	margin : 5px 10px 10px 10px;
	padding : 5px;
	display : block;
}
a:link, a:active, a:visited {
  	margin: 0;
  	padding: 0;
  	color: #073670;
  	text-decoration: none;
}
a:hover {
  	text-decoration: underline;
}
ul {
	margin: 0 auto;
	padding: 0;
	list-style: none;
}
li.menu {
	position: relative;
	padding-left: 5px;
}
li:hover {
	display: block;
}
li.menu a, li.menu a:active, li.menu a:visited {
	width: 20em;
	padding : 0 .5em;
    text-decoration : none;
	display: block;
}
li.menu a:hover {
    text-decoration : underline;
}
pre {	/* style definition for the boxes that show the code samples */
	margin: 0 auto;
	padding: 0;
	border: 1px solid red;
	background-color: #ffffff;
	color: #000000;
	font-size: 1.2em;
	height: 200px;
	width: 80%;
	overflow: auto;
}
#page {
  	margin: 0 auto;
  	padding: 0;
  	width: 100%;
	height: 100%;
  	background-color: #87b7f3;
}
#outer {
  	margin: 0 auto;
  	width: 100%;
  	height: 100%
}
#header {
  	margin: 0 auto;
  	padding: 0;
  	width: 100%;
  	height: 150px;
  	background: #87b7f3 url(../images/backgradient.png) repeat-x;
}
#main {
	margin: 0 auto;
	padding: 0;
	width: 80%;
}
#leftcol {
  	margin: 0;
  	padding: 0;
  	float: left;
  	width: 30%;
}
.nav {
	margin: 0;
	padding: 0;
}
#rightcol {
  	margin: 0;
  	padding: 5px;
  	width: 68%;
  	border: 1px solid #000000;
  	background-color: #bed7f6;
  	float: right;
}
/* contact form styles */
.contactform {
  	margin: 0;
  	padding: 0;
  	width: 100%;
  	line-height: 1.2em;
  	font-size: .9em;
}
form p {
 	margin: 0 auto;
  	width: 400px;
  	font-weight: bold;
  	text-align: right;
  	clear: both;
}
form p label {
  	float: left;
  	width: 100px;
}
form p input, form p textarea {
  	float: left;
}
form p.formbuttons {
  	margin: 0 auto;
  	width: 125px;
}
/* end contact form section */

#commentary {
  	position: relative;
  	width: 100%;
  	background-color: #87b7f3;
}
#footer {
  	width: 800px;
	height: auto;
  	margin: 25px auto;
  	padding: 0;
	font-size: .7em;
	background-color: #87b7f3;
	color: #073670;
	position: relative;
	bottom: 0;
	clear: both;
}
.copyright {
	margin: 0;
	padding: 0 0 0 5px;
	position: relative;
	float: left;
	width: 49%;
}
.madeby {
  	margin: 0;
  	padding: 0 5px 0 0;
  	float: right;
  	text-align: right;
  	width: 49%;
}
#ffscrollbarfix {
  	position: absolute;
  	top: 0;
  	bottom: -0.1px;
  	width: 1em;
  	z-index: -1;
}												

Here is most of the stylesheet that was used for the old version of this website which is a CSS tableless layout design.

Both styles have their places/uses in a website.

 

JavaScript Code Samples

I will be the first to admit that JavaScript isn't my strongest language. That's mainly because I haven't had the opportunity to really get in to it. I plan to learn a lot more about JavaScript in the near future, though, as I teach myself more about AJAX.


I have used it mainly for things like form validation and dynamically hiding or displaying various fields or areas of a web page.


//This function is for checking that an email address
//is in the correct form for an email address.
function checkEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)){ return true; }else{ return false; }
}

//This function is for checking that the required fields
//in a contact form actually have some kind of value in them.
function checkContactForm(f){
	if (f.name.value==""){
		alert("Please enter your name.");
		f.name.select();
		return false;
	}

	if ((f.email.value=="") && (f.phone.value=="")){
		alert("Please, either enter your email address or phone number so that we have a way to contact you back.");
		f.email.select();
		return false;
	}

	if (f.message.value==""){
		alert("Please enter your message.");
		f.message.select();
		return false;
	}

	if (checkEmail(f.email.value)==false){
		alert("Please ensure your Email Address is valid.");
		f.email.select();
		return false;
	}

	return true;
}
												

This code is simply for a contact form validation. For most of the fields I'm only interested in if there is actually data in the field. However for the email field I at least want to make sure that not only is there an email address entered but that it conforms to the standard format for an email address.

 
// display the license issuing entity fields
function dispLicense() {
  	if(document.raffleinfo.licensed.checked == true) {
		document.getElementById('licenseinfo').style.display = "inline";
		grablic('licenseinfo',document.raffleinfo.entity.options[document.raffleinfo.entity.selectedIndex].value)				}
	else
		hideLicense();
}
			
// hide the license issuing entity fields
function hideLicense() {
	document.getElementById('licenseinfo').style.display = "none";
}
												

Here I am using JavaScript to either hide or display some fields on the screen based on wether or not a checkbox field is checked.

 

AJAX Code Samples

AJAX is a technology I have only recently begun teaching myself but I like it a lot. I can definitely see it's uses.

//functions.js

//Create a boolean variable to check for a valid IE instance.
var xmlhttp = false;

//Check if we are using IE.
try {
  	//If the javascript version is greater than 5.
  	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
  		//If not, then use the older active x object.
  		try {
  	  		//If we are using IE.
  	  		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  			} catch (E) {
  	  			//Else we must be using a non-IE browser.
  	  			xmlhttp = false;
  			}
		}

//If we are using a non-IE browser, create a JavaScript instance of the object.
if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}

//Function to change the license entity fields which are normally hidden
function grablic(thelic, theid) {
	//Run AJAX to populate the license entity fields
	serverPage = "get_lic_entity.php?eid=" + theid;
	var obj = document.getElementById(thelic);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}
												

This code is for generating the XMLHTTP Request that is basically the heart of AJAX. It's what allows AJAX to do send and receive requests to the server without having to refresh the screen. In this bit of code I first try to determine if the users browser is an Internet Explorer version greater than 5 because if it's not then the XMLHTTP request needs to be generated in the old way. If neither of those ways actually generate the request then the user must be using something other than IE.

The function at the bottom is the actual function that sends the request for what I want to accomplish with AJAX. When the user changes the selection in a drop down select box about four other fields change their values to correspond to the new selection. The function calls a php file passing it a variable in the url. The response from the php file is then displayed, all without refreshing the screen.

Voodoo Preview
 
Voodoo Preview
 

Here are a couple of screen shots of this in action. The field labeled Entity is a drop down select box. If a different value is selected from that box, the fields directly below it will change their values to correspond to the new selection (the second image).


I also recently helped out another site called Crash Games. The owner had acquired the free version of an AJAX based chat program and he wanted it to be integrated with his current website, or to be more exact to be integrated with the phpBB forums that he set up on his website. On his website, when you register you can join a team. He wanted it so that when you are in the chat program, you can only see those members signed in to the chat program that are on your team, including only those messages in the chat window from members of the same team. I was able to accomodate his request.