code samples

This section contains JavaScript, PHP and C++ code samples written by me, web site author David Glatzl. The purpose of this code sample portfolio is to demonstrate my working knowledge of these programming languages.

JavaScript

				
						/* Start JavaScript */
<script>
	     $(document).ready(function() {
		
		 $('.accordion .term').click(function () {
		 
		  if ($(this).next('p').is(':hidden')) {
		 		  
		 $('.accordion p').slideUp('fast');
          };
		 
		 $(this).next('p').slideToggle('fast');  
            
            });   
        
        });
        
</script>   
/* End JavaScript */
	

            

				
			
				
					<input type="checkbox" name="checkbox" value="check" id="agree" /> <em>By using this website you have read and agree to our <a href="http://simplysacredoils.com/images/Code-Sample-Blank-Form.pdf" target="_blank" rel="noopener noreferrer">Terms of Use</a></em>

/* Start JavaScript */

<form action="https://www.paypal.com/cgi-bin/webscr" onsubmit="if(document.getElementById('agree').checked) { return true; } else { alert('Please indicate that you have read and agree to our Terms of Use'); return false; }">
    
/* End Javascript */
    
<input type="hidden" name="cmd" value="_s-xclick">

<input type="hidden" name="hosted_button_id"value="QK47DXLJ9NTPW">

<input type="image" src="https://www.dgsddpro.com/wp-admin/images/addedFunctionButton1.png" border="0" name="submit" alt="My button">

<img alt="addedFunctionButton1" border="0" src="https://www.dgsddpro.com/buttonGraphic.png" width="1" height="1">
</form>


				
			
				
					/* Start JavaScript */
<script>
    //Search a specific site rather than the entire Web.
    function search() {
      //Replace sample domain name below with your own domain name.
      var site = "dgsddpro.com";
      //Get the text that the user typed into the textbox.
      var lookfor = document.getElementById("txtlookfor").value;
      //If the box wasn't empty do the search. 
      if (lookfor.length > 0) {
        //Build URL for the search.
        var query = "http://www.google.com/search?q=" + encodeURIComponent(lookfor) + " site:" + site;
        //Set Address bar equal to query.
        location.href = query;
      } else {
        //If textbox was empty, show an alert.
        alert("Please type the word or words for which you want to search.");
      }
    }
</script>
/* End JavaScript */

				
			

PHP

An associative array with a while loop and for/each loop.

Project: bulleted list
				
					<?php

$military = array("\n . Leader\n . Beretta hand gun\n . Four star\n\n\n" => "General:", "\n . Second rank\n . Glock hand gun and M27 rifle\n . Silver collar insignia \n\n\n" => "First Lieutenant:", "\n . Third rank\n . M27 rifle and Remington shotgun\n . Embroidered green on red badge\n\n\n" => "Sergeant:", "\n . Fourth rank\n . M27 rifle and Marine sword\n . Embroidered gold on red badge\n\n\n" => "Corporal:"); 

$i = 0;

while( $i < sizeof( $military ) )

$i++;

foreach ( $military as $attribute => $ninja ) {
    echo "Attributes of the $ninja \n $attribute";
}

?>
				
			

An associative array with two - for/each loops.

Project: categorized information
				
					<?php

$military = array(
    'GENERAL' => array('    Role' => 'Leader', '    Weapon' => 'Beretta hand gun', '    Rank' => 'Four star' . "\n\n\n"),
    'FIRST LIEUTENANT' => array('    Role' => 'Second in command',  '    Weapon' => 'Glock hand gun and M27 rifle', '    Rank' => 'Silver collar Insignia' . "\n\n\n"),
    'SERGEANT' => array('    Role' => 'Third in command', '    Weapon' => 'M27 rifle and Remington shotgun', '    Rank' => 'Embroidered green on red badge' . "\n\n\n"),
    'CORPORAL' => array('    Role' => 'Fourth in command', '    Weapon' => 'M27 rifle and a Marine sword', '    Rank' => 'Embroidered gold on red badge' . "\n\n\n")
);


foreach ( $military as $ninja => $attribute ) {
    echo "$ninja-\n\n";

foreach ( $attribute as $label => $attr ) {
        echo "$label: $attr\n";

    }
 }

?>
				
			
				
					<?php

$solution = array(
$Algebra =  6 + 220 * 311 - 1,
$Algebra2 = 22 / ( 10 * 6 - 32 ) * 4,
$Algebra3 = 30 * ( 100 / 5 - 23 * 2 + 9 * 339)
);

print_r($solution);

?>
				
			

Numeric Array

Project: Larger Array with Algebra
				
					<?php

$x = 5;
$y = 21;
$z = 44;

$solution = array(
$Algebra =  6 + 220 + 311 - 1 / $x,
$Algebra2 = 3 / 10 * (6 - 32) % 20,
$Algebra3 = 30 * ( 100 / 5)  - 23 * 2 + (9 * 339),
$Algebra4 = 3 % (70 * 2) - 10 + (8 + 12 * 2) + 1 + 1 -22,
$Algebra5 = ($x + 50) + ($y - 3) - 3 + $y / (4 * $x),
$Algebra6 = ($x - 120) - ($y + 2) + ($z + 1.2) - ( $x % 281) - ( 5 - $x) + $z,
);

echo "Question #1 \n";
echo "6 + 220 + 311 - 1 / $x = " . $solution[0] . "\n\n\n";
echo "Question #2 \n";
echo "3 / 10 * (6 - 32) % 20 = " . $solution[1] . "\n\n\n";
echo "Question #3 \n";
echo "30 * (100 / 5) - 23 * 2 + (9 * 339) = " . $solution[2] . "\n\n\n";
echo "Question #4 \n";
echo "3 % (70 * 2) - 10 + (8 + 12 * 2) + 1 +1 - 22 = " . $solution[3] . "\n\n\n";
echo "Question #5 \n";
echo "($x + 50) + ($y - 3) - 3 + $y / (4 * $x) = " . $solution[4] . "\n\n\n";
echo "Question #6 \n";
echo "($x - 120) - ($y + 2) + ($z + 1.2) - ($x % 281) - (5 - $x) + $z = " . $solution[5] . "\n\n";

?>
				
			

Function

Project: This is a function that compares two integers and prints out the greater of the two. Below, there are three separate pairs of integers that are compared by this function.
				
					<?php
function integer_comparison( $num1, $num2) {

    if ($num1 > $num2) {
        return $num1;

    } else if($num2 > $num1) {
        return $num2;
}

}

var_dump( integer_comparison( 2219, 4196 ) );

var_dump( integer_comparison( 23, 22) );

var_dump( integer_comparison(743, 734 ) );

?>
				
			

c++

Bubble Sort Algorithm

Project: This C++ algorithm can take any group of ten numbers and put them in descending order from the highest number to the lowest number. On line fifteen in the program below, you can see a group of ten numbers in any random order.

When you look at the output of this program you can see the group of numbers have been put in decsending order starting from the highest number to the lowest number.
				
					// Bubblesort Algorithm.cpp 
// Author David Glatzl

#include <iostream>

using namespace std;

const int MAXSIZE = 10;

void bubbleSort(int arr[], int size);
void swap(int& x, int& y);

int main()
{
	int nums[] = { 1, 7, 5, 3, 15, 11, 13, 17, 21, 19 };
	int k;

	cout << "BEFORE SORT: ";
	for (k = 0; k < MAXSIZE; k++)
		cout << nums[k] << " ";

	bubbleSort(nums, MAXSIZE);

	cout << endl << endl;
	cout << "AFTER SORT: ";
	for (k = 0; k < MAXSIZE; k++)
		cout << nums[k] << " ";

	cout << endl << endl << endl;

	system("PAUSE");
	return 0;
} // end main()

void bubbleSort(int arr[], int size)
{
	int last = size - 2;
	int isChanged = 1;

	while (last >= 0 && isChanged)
	{
		isChanged = 0;

		for (int k = 0; k <= last; k++)
			if (arr[k] < arr[k + 1])
			{
				swap(arr[k], arr[k + 1]);
				isChanged = 1;
			}
		last--;
	}
}// end bubbleSort()

void swap(int& x, int& y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}// end swap()
				
			

Array Sort User

Project: This program sorts an array of numbers in ascending order from the lowest number to the highest number.

Then, after the numbers are sorted, the program will ask the user to enter any number that is within the array.

After that, the index number of the element (the number that the user chose) will output or be displayed in the console window.
				
					// File: ArrayUser.cpp
// a pgm that sorts an array and asks the user to find an element by calling the index number.

// Author: David Glatzl

#include<iostream>
#include<stdlib.h>
using namespace std;

const int MAXSIZE = 10;

void sortArr( int arg[], int size );
void swap( int& x, int& y );
int findIndex( int x[] );

int main()
{
	int num[10] = { 45, 9, 23, 100, 90, 6, 86, 17, 3, 5 };
	int i;
	int index;
	
	cout << endl;
	cout << "The elements in the array before they are sorted: ";
	for( i = 0; i < MAXSIZE; ++i )
		cout << num[i] << " ";

	sortArr( num, MAXSIZE );

	cout << endl << endl;
	cout << "After the elements are sorted: ";
	for( i = 0; i < MAXSIZE; ++i )
		cout << num[i] << " ";

	cout << endl << endl;
	
	for( i = 0; i < 1; ++i )
	{
		cout << "Enter any number within the array: ";
		cin >> num[i];
	}

	index = findIndex( num );

	cout << endl;
	cout << "The index for the element you entered is: " << num[index] << endl;
	cout << endl;
	return 0;
}

void sortArr( int arg[], int size )
{
	int last = size - 1;
	int isChanged = 1;

	while (last >= 0 && isChanged)
	{
		isChanged = 0;

		for(int i = 0; i <= last; ++i)
			if(arg[i] < arg[i-1])
			{
				swap(arg[i], arg[i-1]);
				isChanged = 1;
			}
			last--;
	}
}

void swap(int& x, int& y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}

int findIndex( int x[] )
{	
	int i = 0;
	for( i = 0; i < 10; ++i )
	{
		if( x[i] == 3 )
			{
				x[i] = 0;
				break;
			}
		if( x[i] == 5 )
			{
				 x[i] = 1;
				 break;
			}	
		if( x[i] == 6 )
			{
				 x[i] = 2;
				 break;
			}
		if( x[i] == 9 )
			{
				x[i] = 3;
				break;
			}
		if( x[i] == 17 )
			{
				x[i] = 4;
				break;
			}
		if( x[i] == 23 )
			{
				x[i] = 5;
				break;
			}
		if( x[i] == 45 )
			{
				x[i] = 6;
				break;
			}
		if( x[i] == 86 )
			{
				x[i] = 7;
				break;
			}
		if( x[i] == 90 )
			{
				x[i] = 8;
				break;
			}
		if( x[i] == 100 )
			{
				x[i] = 9;
				break;
			}
		}
		return 0;
}