<?php

if (isset($_GET['id']))
{
	$id = abs((int)$_GET['id']);
	
	$sql = "SELECT *,
		((`hit_head` + `hit_chest` + `hit_stomach` + `hit_leftarm` + `hit_rightarm` + `hit_leftleg` + `hit_rightleg`)/`shoot`) AS 'acc'
		FROM `zp_weapon_stat` WHERE`id_weapon` = $id";
	$r = mysql_query($sql);
	$row = mysql_fetch_assoc($r);
	if ($row)
	{		$template = 'weapon.xsl';
		
		$weapon = $root->appendChild($data->createElement('weapon'));
		
		foreach ($row as $key => $value)
			$weapon->appendChild($data->createElement($key, strlen($value) > 0 ? $value : '0.0000'));
			
		$sql = "SELECT * FROM `zp_shoots`, `zp_players` WHERE
			`zp_shoots`.`id_player` = `zp_players`.`id` AND `zp_shoots`.`id_weapon` = $id ORDER BY `kills` DESC LIMIT 10";
		$r = mysql_query($sql);
		$node = $data->createElement('players');
		$players = $root->appendChild($node);
		while ($row = mysql_fetch_assoc($r))
		{
			$player = $players->appendChild($data->createElement('player'));
			foreach ($row as $key => $value)
				$player->appendChild($data->createElement($key, $value));
		}
	}
	else
	{
		$template = 'error.xsl';
		$error = $lang->getElementsByTagName('error_no_weapon')->item(0)->nodeValue;
		$root->appendChild($data->createElement('error', $error));
	}
}
else
{
	$template = 'weapons.xsl';

	$asort = array('name', '`kills`', '`shoot`', '`hit_head`', '`hit_chest`', '`hit_stomach`', '`hit_leftarm`', '`hit_rightarm`', '`hit_leftleg`', '`hit_rightleg`');

	$atsort = array('ASC', 'DESC');	
		
	if (isset($_GET['sort']))
	{
		$sort = abs((int)$_GET['sort']);
		if ($sort > (count($asort) - 1))
			$sort = 0;
		$tsort = $asort[$sort];
		
		if (isset($_SESSION['wtsort']))
		{
			$_SESSION['wtsort'] = $_SESSION['wtsort'] ? 0 : 1;
		}
		else
			$_SESSION['wtsort'] = 0;
		
		$_SESSION['wsort'] = $tsort.' '.$atsort[$_SESSION['wtsort']];
	}

	if (isset($_SESSION['wsort']))
		$wsort = $_SESSION['wsort'];
	else
		$wsort = '`kills` ASC';

	$sql = "SELECT *,
		((`hit_head` + `hit_chest` + `hit_stomach` + `hit_leftarm` + `hit_rightarm` + `hit_leftleg` + `hit_rightleg`)/`shoot`) AS 'acc'
		FROM `zp_weapon_stat` ORDER BY $wsort";

	$r = mysql_query($sql);

	$node = $data->createElement('weapons');
	$weapons = $root->appendChild($node);
	while ($row = mysql_fetch_assoc($r))
	{
		$weapon = $weapons->appendChild($data->createElement('weapon'));
		foreach ($row as $key => $value)
			$weapon->appendChild($data->createElement($key, $value));
	}
}
?>

$r may return false if there is an error in your query. In that case your mysql_fetch_assoc will return the error message posted in your title.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.