<?php

$hostname = "localhost";
$userdb   = "test2";
$passdb   = "webmaster";
$database = "test2";

$temp_table = "cort_temp";
$menu_table = "cort_menu";

function db_login() {
		global $hostname, $userdb, $passdb, $database;
			if (!@$linkdb = mysql_pconnect ($hostname, $userdb, $passdb)) {echo mysql_error();}
			if (!@mysql_select_db ($database)) {echo mysql_error();}
	}



function include_temp($name) {
	global $temp_table, $PHP_SELF, $HTTP_GET_VARS, $HTTP_POST_VARS;
	
reset ($HTTP_POST_VARS);
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
    ${$key} = $val;
}

reset ($HTTP_GET_VARS);
while (list ($key, $val) = each ($HTTP_GET_VARS)) {
    ${$key} = $val;
}
	
		$res = mysql_query("SELECT * FROM $temp_table WHERE name='$name'");
			$type = mysql_result($res, 0, 'type');
			$template = mysql_result($res, 0, 'body');
				if ($type==1) {
					eval ($template);
				} else {
					echo $template;
				}

}

function tree($id_cat, $spacer) {
		global $menu_table, $PHP_SELF;
			$select_cat = "select * from $menu_table where pid = \"$id_cat\""; 
			$sub_cat_query = mysql_query($select_cat); 

			while ($cat_row = mysql_fetch_array($sub_cat_query)) { 
				$id_category = $cat_row['id']; 
				$sub_cat_assign = $cat_row['pid']; 
				$cat_name = $cat_row['name'];
				$id_link = $cat_row['link'];

				// Get results and echo 

				if (strlen($spacer)==0) {echo "<A class=link-head href=\"$PHP_SELF?topic=$id_link\">$spacer$cat_name</a><br>"; }
				if (strlen($spacer)==12)   {echo "<A class=link-sub href=\"$PHP_SELF?topic=$id_link\">$spacer<b>$cat_name</b></a><br>";	}
				if (strlen($spacer)==24)   {echo "<A class=link-sub href=\"$PHP_SELF?topic=$id_link\">$spacer$cat_name</a><br>";	}
				
				
				/*echo "<option  value=\"$id_category\">";
				echo "$spacer $cat_name<br>"; 
				echo "</option>\n";
*/
				// Check to see if category has any children 
				$check_cat_assign = "SELECT id FROM $menu_table WHERE pid = \"$id_category\""; 
				$check_query = mysql_query($check_cat_assign); 
				$check_row = mysql_num_rows($check_query); 

				// If cat has child then call function 
				if ($check_row) { 
					// Add some -- to show the indenting 
					$space = $spacer; 
					$space .= "&nbsp;&nbsp;"; 
					tree($id_category,$space); 

				}


			}

	} 


function tree_op($id_cat,$spacer) {
		global $menu_table;
			$select_cat = "select * from $menu_table where pid = \"$id_cat\""; 
			$sub_cat_query = mysql_query($select_cat); 

			while ($cat_row = mysql_fetch_array($sub_cat_query)) { 
				$id_category = $cat_row['id']; 
				$sub_cat_assign = $cat_row['pid']; 
				$cat_name = $cat_row['name']; 

				// Get results and echo 

				echo "<option  value=\"$id_category\">";
				echo "$spacer $cat_name<br>"; 
				echo "</option>\n";

				// Check to see if category has any children 
				$check_cat_assign = "SELECT id FROM $menu_table WHERE pid = \"$id_category\""; 
				$check_query = mysql_query($check_cat_assign); 
				$check_row = mysql_num_rows($check_query); 

				// If cat has child then call function 
				if ($check_row) { 
					// Add some -- to show the indenting 
					$space = $spacer; 
					$space .= "- "; 
					tree_op($id_category,$space); 

				}


			}

	} 
	
	//Удаление ветви дерева
	function deltree($id_cat) {	
		global $menu_table, $delquery;
			$select_cat = "SELECT * FROM $menu_table WHERE pid = '$id_cat'"; 
			$sub_cat_query = mysql_query($select_cat); 

			while ($cat_row = mysql_fetch_array($sub_cat_query)) { 
				$id_category = $cat_row['id']; 
				$sub_cat_assign = $cat_row['pid']; 
				$cat_name = $cat_row['name']; 
				array_push ($delquery, $id_category);
				$check_cat_assign = "SELECT id FROM $menu_table WHERE pid = \"$id_category\""; 
				$check_query = mysql_query($check_cat_assign); 
				$check_row = mysql_num_rows($check_query); 
			
				if ($check_row) { 
					deltree($id_category); 
				}
			}
		return $delquery;
	}

?>