'.$new_query.'
';
	return ''; // Leerer String, also kein Fehler!
}
// Funktion erstellt die Optionen die Auswahl
// Returns items mit Anzahl die der User verkaufen kann
// @Return String-Array
function getItemOptions($userid,$preselected){
	$result = NULL;
	$index = 0;
	// Die etwas abgewandelten SQL-Querys aus dem Onlinemarkt (MEGA-QUERY)
	$query = 'SELECT i.id, i.name, count(i.id) AS anzahl, \'ware\' as tablename FROM ware w INNER JOIN item i ON(i.id=w.item_id) WHERE w.user = '.$userid.' AND s_type = \'Trank\' GROUP BY i.id
					union
					SELECT i.id, i.name,count(i.id) AS anzahl, \'sp_ware\' as tablename FROM sp_ware w INNER JOIN sp_item i ON(i.id=w.item) WHERE w.user = '.$userid.' GROUP BY i.id
					union
					SELECT i.id, i.item AS name, count(i.id) AS anzahl, \'wochen_ware\' as tablename FROM wochen_ware w INNER JOIN wochen_markt i ON(i.id=w.item) WHERE w.user = '.$userid.' GROUP BY (i.id)';
	
	$qry = mysql_query($query);
	// Damit waeren alle noetigen Datenbankaufrufe erledigt!
	while($row = mysql_fetch_assoc($qry)){
		if($row[id] == $preselected){
			$result[$index++] = '';
		}
		else{
			$result[$index++] = '';
		}
	}
	return $result;
}
//Liefert Zusatzinformationen zu einer Waren-Datenbank
// Returns Array mit DatenbankName, ItemDatenbankName, itemFeldName
// @Return Array
function getDatabaseAdditions($db){
	$returnValue = Array(3);
	$returnValue[0] = $db;
	if($db == 'ware'){$returnValue[1] = 'item';$returnValue[2]='item_id';$returnValue[3]='name';}
	else if($db == 'sp_ware'){$returnValue[1] = 'sp_item';$returnValue[2]='item';$returnValue[3]='name';}
	else if($db == 'wochen_ware'){$returnValue[1] = 'wochen_markt';$returnValue[2]='item';$returnValue[3]='item';}
	return $returnValue;
}
// Diese Funktion stellt ein item in den Basar
// Returns '' wenn alles ok, ansonsten eine Fehlerausgabe
// @Return String
function insertItem($userid,$id_db_mix, $anzahl, $startgebot){
	if(!is_numeric($startgebot) || floor($startgebot) <=0){
		return 'Es muss ein gültiges Gebot abgegeben werden.';
	}
	$startgebot = floor($startgebot);
	if(!is_numeric($anzahl) || floor($anzahl) <= 0){
		return 'Es muss eine gültige Anzahl angegeben werden.';
	}
	$anzahl = floor($anzahl);
	
	preg_replace('#,#','',$id_db_mix); // Entferne die Gefaehrlichen \' !!!
	
	$mix_explode = explode(',',$id_db_mix);
	$id = $mix_explode[0]; // ID des items
	$db = getDatabaseAdditions($mix_explode[1]); // Datenbank die das Item speichert, mit samt den Zusatzinformationen abholen
	
	// Nun die Anzahl vergleichen
	$test = 'SELECT count(i.id) AS anzahl FROM '.$db[0].' as w INNER JOIN '.$db[1].' as i ON(i.id=w.'.$db[2].') where w.user= '.$userid.' AND i.id = '.$id;
	$result = mysql_fetch_assoc(mysql_query($test));
//		echo $test;
	if($result['anzahl'] < $anzahl){
		return 'Nicht genug Items (Vorhanden:'.$result['anzahl'].', Eingetragen:'.$anzahl.')';
	}
	$test = 'SELECT '.$db[3].' AS name FROM '.$db[1].' WHERE id ='.$id;
	$itemname = mysql_fetch_assoc(mysql_query($test));
//	echo $test;
	// Nun werden dem User die Items abgezogen und in die auktionstabelle eingefuegt.
	$qry = mysql_query('DELETE FROM '.$db[0].' WHERE '.$db[2].'='.$id.' AND user='.$userid.' LIMIT '.$anzahl);
	$anzahl2 = mysql_affected_rows();
	if($anzahl2 == 0){
		return 'Cheater!!';
	} else if($anzahl != $anzahl2){
		mysql_query('INSERT INTO auktion (itemid, itemname, tablename, anbieter, anzahl, deadline, startgebot, startdate) VALUES('.$id.',\''.$itemname['name'].'\',\''.$db[0].'\','.$userid.','.$anzahl2.',TIMESTAMPADD(Day,3,TIMESTAMPADD(Second,-second(now()),now())),'.$startgebot.',now())');
	} else{
		mysql_query('INSERT INTO auktion (itemid, itemname, tablename, anbieter, anzahl, deadline, startgebot, startdate) VALUES('.$id.',\''.$itemname['name'].'\',\''.$db[0].'\','.$userid.','.$anzahl.',TIMESTAMPADD(Day,3,TIMESTAMPADD(Second,-second(now()),now())),'.$startgebot.',now())');
	}
	return '';
}
// Diese Funktion erstellt die Auktionstabelle und die Transaktionentabelle
function createTables(){
	// Erstellen der Auktionstabelle
	mysql_query('DROP TABLE auktion');
	mysql_query('
		CREATE TABLE `auktion` (
			`auktionsid` int(10) unsigned NOT NULL auto_increment,
			`anbieter` int(10) unsigned NOT NULL,
			`bieter` int(10) unsigned default NULL,
			`itemid` int(10) unsigned NOT NULL,
			`itemname` varchar(20) collate utf8_unicode_ci NOT NULL,
			`tablename` varchar(15) collate utf8_unicode_ci NOT NULL,
			`anzahl` int(10) unsigned NOT NULL,
			`startgebot` decimal(10,0) unsigned NOT NULL,
			`aktuellesgebot` decimal(10,0) unsigned default NULL,
			`deadline` timestamp NULL default NULL,
			`startdate` timestamp NULL default NULL,
			`cheatingverdacht` int(10) unsigned NOT NULL,
			PRIMARY KEY  (`auktionsid`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;
	');
	mysql_query('DROP TABLE auktion_transaktionen');
	mysql_query('
		CREATE TABLE `auktion_transaktionen` (
			`transaktionsid` int(10) unsigned NOT NULL auto_increment,
			`anbieter` int(10) unsigned NOT NULL,
			`bieter` int(10) unsigned NOT NULL,
			`itemid` int(10) unsigned NOT NULL,
			`tablename` varchar(15) collate utf8_unicode_ci NOT NULL,
			`anzahl` int(10) unsigned NOT NULL,
			`betrag` decimal(10,0) unsigned NOT NULL,
			`deadline` timestamp NULL default NULL,
			`cheatingverdacht` int(10) unsigned NOT NULL,
			PRIMARY KEY  (`transaktionsid`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;
	');
}
function getAuktionTotalCount($kategorie,$itemnamepart,$maxPrice){
	// vorbereiten der uebergabevariablen zu sicherheitszwecken
	
	$priceDelimiter = '';
	$kategorieDelimitier ='';
	$stringDelimiter = '';
	if($maxPrice > 0){
		$priceDelimiter = ' AND startgebot <= '.$maxPrice.' AND (aktuellesgebot IS NULL OR aktuellesgebot <= '.$maxPrice.') ';
	}
	if($kategorie == 'wochen_ware' OR $kategorie == 'ware' OR $kategorie == 'sp_ware'){
		$kategorieDelimitier = ' AND tablename = \''.$kategorie.'\' ';
	}
	if($itemnamepart != NULL && $itemnamepart != ''){
		$stringDelimiter = ' AND itemname LIKE \'%'.$itemnamepart.'%\' ';
	}
	$test = 'SELECT count(auktionsid) as anzahl FROM auktion WHERE 1 '.$kategorieDelimitier.$stringDelimiter.$priceDelimiter.' AND TIMESTAMPDIFF(Minute,now(),deadline) >= 0';
	$qry = mysql_query($test);
	if(mysql_num_rows($qry) == 0){
		return 0;
	}
	$result = mysql_fetch_assoc($qry);
	return $result['anzahl'];
}
function getAuktionEntries($kategorie,$itemnamepart,$entriesPerSite,$pageNumber,$maxPrice,$order,$dir){
	// vorbereiten der uebergabevariablen zu sicherheitszwecken
	$priceDelimiter = '';
	$kategorieDelimitier ='';
	$stringDelimiter = '';
	$interval = ' LIMIT '.($entriesPerSite*$pageNumber).','.$entriesPerSite.' ';
	$returnArray = NULL;
	if($maxPrice > 0){
		$priceDelimiter = ' AND startgebot <= '.$maxPrice.' AND (aktuellesgebot IS NULL OR aktuellesgebot <= '.$maxPrice.') ';
	}
	if($kategorie == 'wochen_ware' OR $kategorie == 'ware' OR $kategorie == 'sp_ware'){
		$kategorieDelimitier = ' AND tablename = \''.$kategorie.'\' ';
	}
	if($itemnamepart != NULL && $itemnamepart != ''){
		$stringDelimiter = ' AND itemname LIKE \'%'.$itemnamepart.'%\' ';
	}
	$index = 0;
	$test = 'SELECT a.auktionsid, u.nickname AS anbietername, u2.nickname AS bietername, a.deadline, a.aktuellesgebot, a.startgebot, a.anzahl, a.itemname, a.bieter, a.anbieter, a.itemid, a.tablename FROM auktion AS a INNER JOIN user as u ON a.anbieter = u.id LEFT JOIN user as u2 ON a.bieter = u2.id WHERE 1 '.$kategorieDelimitier.$stringDelimiter.$priceDelimiter.' AND TIMESTAMPDIFF(Minute,now(),deadline) >= 0 ORDER BY '.$order.' '.$dir.' '.$interval;
	$qry = mysql_query($test);
	//echo '
'.$test.'
';
	while($result = mysql_fetch_assoc($qry)){
		$returnArray[$index++] = $result;
	}
	return $returnArray;
}
function getEntryInformation($auktionsid){
	if(!is_numeric($auktionsid)){return;}
	$qry = mysql_query('SELECT u.nickname AS anbietername, u2.nickname AS bietername, a.auktionsid,a.anbieter,a.bieter,a.itemid,a.itemname,a.tablename,a.anzahl,a.startgebot,a.aktuellesgebot,a.deadline,a.startdate,a.cheatingverdacht, TIMESTAMPDIFF(Minute,now(),deadline) AS zeitdifferenz FROM auktion  AS a INNER JOIN user as u ON a.anbieter = u.id LEFT JOIN user as u2 ON a.bieter = u2.id WHERE auktionsid = '.$auktionsid);
	if(mysql_num_rows($qry) == 0){ return NULL; }
	return mysql_fetch_assoc($qry);
}
function getKategorieOptions($preselect){
	$result = NULL;
	$index = 0;
	$converter['ware'] = 'Tränke';
	$converter['sp_ware'] = 'Items';
	$converter['wochen_ware'] = 'Teufelsfrüchte';
	
	$total = 0;
	$select = false;
	$qry = mysql_query('SELECT tablename,count(tablename) as anzahl FROM auktion WHERE TIMESTAMPDIFF(Minute,now(),deadline) >= 0 GROUP BY tablename');
	while($row = mysql_fetch_assoc($qry)){
		if($preselect == $row['tablename']){
			$result[$index++] = '';
			$select = true;
		} else{
			$result[$index++] = '';
		}
		$total += $row['anzahl'];
	}
	if(!$select){
		$result[$index++] = '';
	} else{
		$result[$index++] = '';
	}
	return $result;
}
function zurueckziehen($userid, $auktionsid, $pay){
	// So das eigentliche abziehen
	$auktionsdaten = getEntryInformation($auktionsid);
	if($userid != $auktionsdaten['anbieter']){
		return 'Sie sind garnicht der Anbieter dieser Auktion!';
	}
	if($pay == 'YES' && $auktionsdaten['aktuellesgebot'] == NULL){
		return 'Es wurde mitübergeben, dass geboten wurde, es ist aber nicht geboten worden!';
	}
	else if($pay == 'NO' && $auktionsdaten['aktuellesgebot'] != NULL){
		return 'Es wurde zwischenzeitlich geboten! Zurückziehen nur mit Gebühr möglich!';
	}
	$gebuehr = floor($auktionsdaten['aktuellesgebot'] * 0.1);
	mysql_query('UPDATE user SET geld=geld-'.$gebuehr.' WHERE id='.$userid);
	mysql_query('DELETE FROM auktion WHERE auktionsid='.$auktionsid);
	$anzahl2 = mysql_affected_rows();
	if($anzahl2 == 0){
		return 'CHEATER!!';
	}
	$anzahl = $auktionsdaten['anzahl'];
	$item_row_name = 'item';
	if($auktionsdaten['tablename'] == 'ware'){$item_row_name = 'item_id';}
	while($anzahl-- > 0){
		$sql = 'INSERT INTO '.$auktionsdaten['tablename'].' ('.$item_row_name.', user) VALUES('.$auktionsdaten['itemid'].','.$userid.')';
		mysql_query($sql);
//			echo '
'.$sql.'
';
	}
	$sql = 'INSERT INTO nachricht(von, besitzer, betreff, text) VALUES (\'Auktionsmarkt\', '.$auktionsdaten['anbieter'].', \'Item zurückgezogen!\', \' Sie haben das Item '.$auktionsdaten['itemname'].' für eine Gebühr von '.$gebuehr.' zurückgezogen!\')';
	mysql_query($sql);
//		echo '
'.$sql.'
';
	if($auktionsdaten['bieter'] != NULL){
		$sql = 'INSERT INTO nachricht(von, besitzer, betreff, text) VALUES (\'Auktionsmarkt\', '.$auktionsdaten['bieter'].', \'Item zurückgezogen!\', \' Das Item '.$auktionsdaten['itemname'].' wurde von '.$auktionsdaten['anbietername'].' zurückgezogen!\')';
		mysql_query($sql);
//			echo '
'.$sql.'
';
	}
}
function getAveragePrice($itemid, $tablename, $anzahl){
	$sql = 'SELECT avg(betrag) / avg(anzahl) AS average FROM auktion_transaktionen WHERE anbieter != 1 and itemid = '.$itemid.' and tablename = \''.$tablename.'\' and cheatingverdacht = 0 and TIMESTAMPADD(DAY,30,deadline) > CURRENT_TIMESTAMP';
	$qry = mysql_query($sql);
	$row = mysql_fetch_assoc($qry);
	return round($row['average']*$anzahl);
}
?>