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

	$tmpl = new CInterface('sample1.tmpl');
	$tmpl->title = 'Address Book';

	$item = (!isset($_GET['item']))?0:$_GET['item'];

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

	$mydata->Connect();
	$mydata->SetQuery('select count(*) from addressbook');
	$count = mysql_fetch_array($mydata->Query());
	$count = $count[0];
	$mydata->Close();

	if(isset($_GET['delete'])&&($count > 0)) {
		$delete = $_GET['delete'];

		$mydata->Connect();

		$mydata->SetQuery('select addressID, Name from addressbook limit '.$delete.',1');
		$id_del = mysql_fetch_array($mydata->Query());
		$id_name = $id_del['Name'];
		$id_del = $id_del['addressID'];

		$mydata->SetQuery('delete from addressbook where addressID = "'.$id_del.'"');
		$mydata->Query();

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

		while($data = mysql_fetch_array($result)) {
			$mydata->SetQuery('delete from detailmember where memid = "'.$data['memid'].'"');
			$mydata->Query();
		}

		$mydata->SetQuery('delete from familymembers where addrid = "'.$id_del.'"');
		$mydata->Query();

		$mydata->Close();

		$tmpl->info_del = '<tr bgcolor="#FF0000">'.
			'<td colspan="8"><div align="center"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">'.
			'<em>Item with Name ['.$id_name.'] has been successfully deleted!</em></font></strong></div></td></tr>';
		$count--;
		$item = ($delete < $count-1)?$delete:$count-1;
	}

	if($count <= 0) {
		// redirect it to the add item page since the item is empty
		header("Location: sample2.php");
		exit;
	}

	$tmpl->first = 0;
	$tmpl->prev = ($item > 0)?($item-1):0;
	$tmpl->next = ($item < $count-1)?($item+1):($count-1);
	$tmpl->last = $count-1;
	$tmpl->item = $item;

	$mydata->Connect();
	$mydata->SetQuery('select * from addressbook limit '.$item.',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'];

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

	function state_selected($row) {
		// declared global, if doesn't, these variables will be localized
		// and you won't get what you expected
		global $tmpl, $addrbook;

		$state = $tmpl->state;
		return ($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();

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

	// prepare for sub-detail data
	function mem($data) {
		// declared global to these variables or it will be localized
		global $mydata, $tmpl;

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

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