<?php



include ('dbConnect.php');



$idate = explode("/", $_POST['date']);



$date= mysqli_real_escape_string($hookup, $idate[2]."-".$idate[0]."-".$idate[1]);



$ui= $_POST['ui'];



if($ui=="get"){



	$codex = mysqli_real_escape_string($hookup, $_POST['codex']);



	$usql = "SELECT * FROM `Traits` WHERE `codex` = '$codex'";

	

	$result = mysqli_query($hookup, $usql);

	$cnt=0;

	$xml="<xml><data>";

	//$row = mysqli_fetch_assoc($result);

	while($row = mysqli_fetch_assoc($result)) {

		$xml.="<item><id>".$row['ID']."</id>";

		$xml.="<title><![CDATA[".$row['title']."]]></title>";

		$xml.="<codex><![CDATA[".$row['codex']."]]></codex>";

		$xml.="<group><![CDATA[".$row['group']."]]></group>";

		$xml.="<rank><![CDATA[".$row['rank']."]]></rank>";

		$xml.="</item>";

	}

	

	$xml.="</data></xml>";

	echo $xml;

	

}else if($ui=="get_all"){





	$usql = "SELECT * FROM `Traits` ORDER BY `group` ASC, `rank` ASC";

	

	$result = mysqli_query($hookup, $usql);

	$cnt=0;

	$xml="<xml><data>";

	//$row = mysqli_fetch_assoc($result);

	while($row = mysqli_fetch_assoc($result)) {

		$xml.="<item><id>".$row['ID']."</id>";

		$xml.="<title><![CDATA[".$row['title']."]]></title>";

		$xml.="<codex><![CDATA[".$row['codex']."]]></codex>";

		$xml.="<group><![CDATA[".$row['group']."]]></group>";

		$xml.="<rank><![CDATA[".$row['rank']."]]></rank>";

		$xml.="</item>";

	}

	

	$xml.="</data></xml>";

	echo $xml;

	

}else if ($ui == "ttreading") {

		

	$codex = mysqli_real_escape_string($hookup, $_POST['codex']);



	$usql = "SELECT * FROM `Traits` WHERE `codex` = '$codex'";

	

	$result = mysqli_query($hookup, $usql);

	$cnt=0;

	$xml="<xml><data>";

	//$row = mysqli_fetch_assoc($result);

	while($row = mysqli_fetch_assoc($result)) {

		$xml.="<item><id>".$row['ID']."</id>";

		$xml.="<title><![CDATA[".$row['title']."]]></title>";

		$xml.="<codex><![CDATA[".$row['codex']."]]></codex>";

		$xml.="<group><![CDATA[".$row['group']."]]></group>";

		$xml.="<rank><![CDATA[".$row['rank']."]]></rank>";

		$xml.="</item>";

	}

	

	$xml.="</data></xml>";

	echo $xml;

	

}else if($ui=="update"){



$id = intval($_POST['id']);

$title = mysqli_real_escape_string($hookup, $_POST['title']);

$codex = mysqli_real_escape_string($hookup, $_POST['codex']);

$group = intval($_POST['group']);

$rank = intval($_POST['rank']);

	

	$sql = "UPDATE `Traits` SET `title` = '$title', `codex` = '$codex', `group` = '$group', `rank` = '$rank' WHERE `ID` = '$id'";

	

	if(mysqli_query($hookup, $sql)){

		$return = "success";

		echo $return; 

	}else{

		$return = "error=" . $type. " / ". mysqli_error($hookup);

		echo $return; 

	 }

	 

}else if($ui=="save"){



	$codex = mysqli_real_escape_string($hookup, $_POST['codex']);

	$title = mysqli_real_escape_string($hookup, $_POST['title']);

	$group = intval($_POST['group']);

	$rank = intval($_POST['rank']);

	

	$sql = "INSERT INTO `Traits` (`ID`, `title`, `codex`, `group`, `rank`, `language` ) VALUES ('', '$title', '$codex' , '$group', '$rank', '0')";

	$result=mysqli_query($hookup, $sql);

	$used_id = "<result>".mysqli_insert_id($hookup)."</result>";

	if($result){

		

		$return = $used_id;

		echo $return; 

	}else{

		$return = "error=" . $type. " / ". mysqli_error($hookup);

		echo $return; 

	 }

}else if($ui=="delete"){



	$id = intval($_POST['id']);

	

	$sql = "DELETE FROM `Traits` WHERE `ID` = '$id'";

	$result=mysqli_query($hookup, $sql);

	$used_id = "<result>".mysqli_insert_id($hookup)."</result>";

	if($result){

		

		$return = "success";

		echo $return; 

	}else{

		$return = "error=" . $type. " / ". mysqli_error($hookup);

		echo $return; 

	 }

}



?>