<?
include_once('mapcfg.php');

echo ("<html>");
echo ("<head>");
echo ("<meta http-equiv=Content-Style-Type content=text/css>");
echo ("<link rel=stylesheet href=$cssloc type=text/css>");
echo ("<title>$D2campname Economy</title>");
echo ("</head>");
echo ("<body");
echo ("<br>");
echo ("<table width=60% cellspacing=0 cellpadding=0 border=0 align=center>");
  	echo ("<tr>");
    	echo ("<td width=100% align=center class=windowbg2>");
	echo ("<font face=Arial size=2>");

				$control = array();
				$econ = array();
				
				$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   				mysql_select_db($D2db);
    				$query = "SELECT EmpireControl,CartelControl,CurrentEconomicPoints FROM maphex";
    				$result = mysql_query($query);
				$a=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
						$control[$a] = $line['EmpireControl'];
						$cartelControl[$a] = $line['CartelControl'];
						$econ[$a] = $line['CurrentEconomicPoints'];
						$a=$a+1;
    				}
    				mysql_free_result($result);
    				mysql_close($link);

				$empecon = array();
				$empecon[0] = 0;
				$empecon[1] = 0;
				$empecon[2] = 0;
				$empecon[3] = 0;
				$empecon[4] = 0;
				$empecon[5] = 0;
				$empecon[6] = 0;
				$empecon[7] = 0;
				$empecon[8] = 0;
				$empecon[9] = 0;
				$empecon[10] = 0;
				$empecon[11] = 0;
				$empecon[12] = 0;
				$empecon[13] = 0;
				$empecon[14] = 0;
				$empecon[15] = 0;
				$empecon[16] = 0;
				$empecon[23] = 0;

				$empcolor = array($empstring[0] => $empcolor[0],
						$empstring[1] => $empcolor[1],
						$empstring[2] => $empcolor[2],
						$empstring[3] => $empcolor[3],
						$empstring[4] => $empcolor[4],
						$empstring[5] => $empcolor[5],
						$empstring[6] => $empcolor[6],
						$empstring[7] => $empcolor[7],
						$empstring[8] => $empcolor[8],
						$empstring[9] => $empcolor[9],
						$empstring[10] => $empcolor[10],
						$empstring[11] => $empcolor[11],
						$empstring[12] => $empcolor[12],
						$empstring[13] => $empcolor[13],
						$empstring[14] => $empcolor[14],
						$empstring[15] => $empcolor[15],
						$empstring[16] => $empcolor[16],
						$empstring[23] => $empcolor[23]);


				$combo1 = array();
				$combo2 = array();
				$r=0;

				for ($i = 0; $i <= $a-1; $i++) {
					if(($control[$i] == 0) or ($cartelControl[$i] == 0)) {
						$empecon[0] = $empecon[0] + $econ[$i];
					}
					if(($control[$i] == 1) or ($cartelControl[$i] == 1)) {
						$empecon[1] = $empecon[1] + $econ[$i];
					}
					if(($control[$i] == 2) or ($cartelControl[$i] == 2)) {
						$empecon[2] = $empecon[2] + $econ[$i];
					}
					if(($control[$i] == 3) or ($cartelControl[$i] == 3)) {
						$empecon[3] = $empecon[3] + $econ[$i];
					}
					if(($control[$i] == 4) or ($cartelControl[$i] == 4)) {
						$empecon[4] = $empecon[4] + $econ[$i];
					}
					if(($control[$i] == 5) or ($cartelControl[$i] == 5)) {
						$empecon[5] = $empecon[5] + $econ[$i];
					}
					if(($control[$i] == 6) or ($cartelControl[$i] == 6)) {
						$empecon[6] = $empecon[6] + $econ[$i];
					}
					if(($control[$i] == 7) or ($cartelControl[$i] == 7)) {
						$empecon[7] = $empecon[7] + $econ[$i];
					}
					if(($control[$i] == 8) or ($cartelControl[$i] == 8)) {
						$empecon[8] = $empecon[8] + $econ[$i];
					}
					if(($control[$i] == 9) or ($cartelControl[$i] == 9)) {
						$empecon[9] = $empecon[9] + $econ[$i];
					}
					if(($control[$i] == 10) or ($cartelControl[$i] == 10)) {
						$empecon[10] = $empecon[10] + $econ[$i];
					}
					if(($control[$i] == 11) or ($cartelControl[$i] == 11)) {
						$empecon[11] = $empecon[11] + $econ[$i];
					}
					if(($control[$i] == 12) or ($cartelControl[$i] == 12)) {
						$empecon[12] = $empecon[12] + $econ[$i];
					}
					if(($control[$i] == 13) or ($cartelControl[$i] == 13)) {
						$empecon[13] = $empecon[13] + $econ[$i];
					}
					if(($control[$i] == 14) or ($cartelControl[$i] == 14)) {
						$empecon[14] = $empecon[14] + $econ[$i];
					}
					if(($control[$i] == 15) or ($cartelControl[$i] == 15)) {
						$empecon[15] = $empecon[15] + $econ[$i];
					}
					if(($control[$i] == 16) or ($cartelControl[$i] == 16)) {
						$empecon[16] = $empecon[16] + $econ[$i];
					}
					if(($control[$i] == 23) or ($cartelControl[$i] == 23)) {
						$empecon[23] = $empecon[23] + $econ[$i];
					}
				}
				
				for ($j = 0; $j <= 23; $j++) {
						if($j <=16 or $j >=23) {
							$combo1 = array($empstring[$j]=>$empecon[$j]);
							$combo2 = array_merge ($combo2 , $combo1);
						}					
				}
				
				arsort ($combo2);
				reset ($combo2);

				echo ("<br>");
				while (list ($key, $val) = each ($combo2)) {
					echo ("The ");
					for ($k = 0; $k <= 23; $k++) {					
						if($k <=16 or $k >=23) {
							if($key == $empstring[$k]) {							
								echo "<b><font color=";
								echo $empcolor[$empstring[$k]];
								echo ">";
							}	
						}
					}
					echo $key;
					echo "</font></b>";
					echo (" ranks at ");
					echo $r+1;
					echo (" with ");
					echo $val;
					echo (" total economic points.");
					echo ("<br><br>");
					$r=$r+1;
				}								
	echo ("</font>");
	echo ("</td>");
	echo ("</tr>");
echo ("</table>");
echo ("<br>");
echo ("</body>");
echo ("</html>");

?>