Location: PHPKode > scripts > Template Interface Engine > template-interface-engine/sample2.php
<?php
	include 'interface.class.php';
	include 'mysql.class.php';

	$tmpl = new CInterface('sample2.tmpl');
	$tmpl->title = 'Address Book';
	$tmpl->save = 'save';
	$tmpl->Save = 'Save';
	$tmpl->item = 0;
	$tmpl->birth = 'YYYY-MM-DD';

	$mydata = new Tmysql;
	$mydata->SetConnection('hostname', 'username', 'password', 'interface_sample');

	$mydata->Connect();
	$mydata->SetQuery('select StateID, State, StateAbbr from states order by State');
	$tmpl->state = mysql_to_array($mydata->Query());
	$mydata->Close();

	if(isset($_GET['edit'])) {
		$tmpl->save = 'change';
		$tmpl->Save = 'Change';
		$tmpl->item = $_GET['edit'];
		$tmpl->hidden_item = '<input type="hidden" name="edit" value="'.$_GET['edit'].'">';
	}
	if(isset($_GET['addmember'])||isset($_GET['addcv'])||
	  isset($_GET['delmember'])||isset($_GET['delcv'])||
	  isset($_GET['save'])||isset($_GET['change'])) {
		$tmpl->name = $_GET['Name'];
		$tmpl->address = $_GET['Address'];
		$tmpl->city = $_GET['City'];
		$tmpl->zip = $_GET['Zip'];
		$tmpl->email = $_GET['Email'];
		$tmpl->phone = $_GET['Phone'];
		$tmpl->birth = $_GET['Birthday'];
		$tmpl->notes = str_replace('\\', '', $_GET['Notes']);
		$tmpl->mem = array_map(null, $_GET['firstname'], $_GET['lastname'], $_GET['age']);
		// arrange the index of these _GET vars start at 0
		$_GET['cvschool'] = array_values($_GET['cvschool']);
		$_GET['cvyear'] = array_values($_GET['cvyear']);

		function mem($data) { // sub-detail setup
			static $nom = 0; // as the data index
			global $tmpl;
			if(isset($_GET['cvschool'][$nom])) {
				$tmpl->det = array_map(null, 
					array_keys(array_fill(1, count($_GET['cvschool'][$nom]), 1)), 
					$_GET['cvschool'][$nom], 
					$_GET['cvyear'][$nom]);
			} else $tmpl->det = array(array("1"));
			if(isset($_GET['addcv'])) {
				$addcv = $_GET['addcv'];
				list($num, ) = each($addcv);
				if($num == $nom+1) $tmpl->det = array_merge($tmpl->det, array(array(count($tmpl->det) + 1)));
			} elseif(isset($_GET['delcv'])) {
				$delcv = $_GET['delcv'];
				list($num, $nummbr) = each($delcv);
				list($numcv, ) = each($nummbr);
				if($num == $nom+1) {
					$arrcv = $tmpl->det;
					unset($arrcv[$numcv-1]);
					$tmpl->det = array_values($arrcv);
					if(!sizeof($tmpl->det)) $tmpl->det = array(array("1"));
				}
			}
			$nom++;	// step-up then index
		}

		function state_selected($row) {
			global $tmpl;
			return ($tmpl->state[$row][0] == $_GET['State'])?'selected':'';
		}

		if(isset($_GET['delmember'])) {	// perform to delete the family member
			list($delmbr, ) = each($_GET['delmember']);
			$delarr = $tmpl->mem;
			unset($delarr[$delmbr-1]);
			unset($_GET['cvschool'][$delmbr-1]);
			unset($_GET['cvyear'][$delmbr-1]);
			// re-arrange the index
			$tmpl->mem = array_values($delarr);
			$_GET['cvschool'] = array_values($_GET['cvschool']);
			$_GET['cvyear'] = array_values($_GET['cvyear']);
			if(!sizeof($tmpl->mem)) $tmpl->mem = array(array());
		} elseif(isset($_GET['addmember'])) { // perform to add the family member
			$tmpl->mem = array_merge($tmpl->mem, array(array()));
		} elseif(isset($_GET['save'])) { // now it perform to save to the database
			$email = str_replace(
				array('@', '-', '_', '.'), 
				array(' at ', ' dash ', ' underscore ', ' dot '), 
				$tmpl->email);

			$mydata->Connect();

			$qry1 = 'insert into addressbook '.
				'(addressID, Name, Address, City, State, Zip, Email, Phone, Birthday, Notes) values '.
				'("%d", "'.$tmpl->name.'", "'.$tmpl->address.'", "'.$tmpl->city.'", "'.$_GET['State'].'", "'.$tmpl->zip.'", "'.$email.'", "'.$tmpl->phone.'", "'.$tmpl->birth.'", "'.$tmpl->notes.'")';
			$ida = 1;
			do {
				$qry2 = sprintf("$qry1", $ida++);
				$mydata->SetQuery($qry2);
				$mydata->Query();
			} while($mydata->ErrorMessage != '');
			$ida--;

			$member = $tmpl->mem;
			foreach($member as $num => $familymbr) {
				$qry1 = 'insert into familymembers (memid, addrid, firstname, lastname, age) values ("%d", "'.$ida.'", "'.$familymbr[0].'", "'.$familymbr[1].'", "'.$familymbr[2].'")';
				static $idf = 1;
				do {
					$qry2 = sprintf("$qry1", $idf++);
					$mydata->SetQuery($qry2);
					$mydata->Query();
				} while($mydata->ErrorMessage != '');
				$idf--;

				$det = array_map(null, $_GET['cvschool'][$num], $_GET['cvyear'][$num]);
				foreach($det as $no => $detail) {
					$qry1 = 'insert into detailmember (memid, No, School, YearOfSchool) values ("'.$idf.'", "'.($no+1).'", "'.$detail[0].'", "'.$detail[1].'")';
					$mydata->SetQuery($qry1);
					$mydata->Query();
				}
			}
			$mydata->Close();

			$ida--;
			// back to the browse page
			header("Location: sample1.php?item=$ida");
			exit;
		} elseif(isset($_GET['change'])) { // perform to change to the updated data
			$edit = $_GET['edit'];

			$mydata->Connect();

			$mydata->SetQuery('select addressID from addressbook limit '.$edit.',1');
			$res = mysql_fetch_array($mydata->Query());
			$id_edit = $res['addressID'];

			$email = str_replace(
				array('@', '-', '_', '.'), 
				array(' at ', ' dash ', ' underscore ', ' dot '), 
				$tmpl->email);

			$qry1 = 'update addressbook set Name="'.$tmpl->name.'", Address="'.$tmpl->address.'", City="'.$tmpl->city.
				'", State="'.$_GET['State'].'", Zip="'.$tmpl->zip.'", Email="'.$email.'", Phone="'.$tmpl->phone.
				'", Birthday="'.$tmpl->birth.'", Notes="'.$tmpl->notes.'" where addressID="'.$id_edit.'"';
			$mydata->SetQuery($qry1);
			$mydata->Query();

			$mydata->SetQuery('select memid from familymembers where addrid="'.$id_edit.'"');
			$result = $mydata->Query();

			// perform to delete members first
			while($res = mysql_fetch_array($result)) {
				$qry1 = 'delete from detailmember where memid="'.$res['memid'].'"';
				$mydata->SetQuery($qry1);
				$mydata->Query();
			}
			$qry1 = 'delete from familymembers where addrid="'.$id_edit.'"';
			$mydata->SetQuery($qry1);
			$mydata->Query();

			// then insert it back with the updated data
			$member = $tmpl->mem;
			foreach($member as $num => $familymbr) {
				$qry1 = 'insert into familymembers (memid, addrid, firstname, lastname, age) values ("%d", "'.$id_edit.'", "'.$familymbr[0].'", "'.$familymbr[1].'", "'.$familymbr[2].'")';
				static $idf = 1;
				do {
					$qry2 = sprintf("$qry1", $idf++);
					$mydata->SetQuery($qry2);
					$mydata->Query();
				} while($mydata->ErrorMessage != '');
				$idf--;

				$det = array_map(null, $_GET['cvschool'][$num], $_GET['cvyear'][$num]);
				foreach($det as $no => $detail) {
					$qry1 = 'insert into detailmember (memid, No, School, YearOfSchool) values ("'.$idf.'", "'.($no+1).'", "'.$detail[0].'", "'.$detail[1].'")';
					$mydata->SetQuery($qry1);
					$mydata->Query();
				}
			}
			$mydata->Close();

			// back to the browse page
			header("Location: sample1.php?item=$edit");
			exit;
		}
	} elseif(isset($_GET['edit'])) { // detect and perform as the edit page instead of the add page
		$edit = $_GET['edit'];

		$mydata->Connect();
		$mydata->SetQuery('select * from addressbook limit '.$edit.',1');
		$addrbook = mysql_fetch_array($mydata->Query());
		$mydata->Close();

		$tmpl->name = $addrbook['Name'];
		$tmpl->address = $addrbook['Address'];
		$tmpl->city = $addrbook['City'];
		$tmpl->zip = $addrbook['Zip'];
		$tmpl->email = $addrbook['Email'];
		$tmpl->phone = $addrbook['Phone'];
		$tmpl->birth = $addrbook['Birthday'];
		$tmpl->notes = $addrbook['Notes'];

		function state_selected($row) {
			global $tmpl, $addrbook;
			return ($tmpl->state[$row][0] == $addrbook['State'])?'selected':'';
		}

		$mydata->Connect();
		$mydata->SetQuery('select firstname, lastname, age, memid from familymembers where addrid = "'.$addrbook['addressID'].'" order by firstname');
		$tmpl->mem = mysql_to_array($mydata->Query());
		$mydata->Close();

		if(count($tmpl->mem)) {
			// prepare for sub-detail data
			function mem($data) {
				global $mydata, $tmpl;

				$mydata->Connect();
				$mydata->SetQuery('select No, School, YearOfSchool from detailmember where memid = "'.$data['memid'].'" order by No');
				$tmpl->det = mysql_to_array($mydata->Query());
				$mydata->Close();
			}
		} else {
			$tmpl->mem = array(array());

			function mem($data) {
				global $tmpl;
				$tmpl->det = array(array("1"));
			}
		}
	} else { // default value
		$tmpl->mem = array(array());

		function mem($data) {
			global $tmpl;
			$tmpl->det = array(array("1"));
		}
	}

	function mem_nom($row) {
		return $row + 1;
	}

	function det_cvnom($row) {
		return $row + 1;
	}

	$tmpl->Output();
?>
Return current item: Template Interface Engine