I have the following code that displays the full contents of an XML page. I would like to limit this information to just the "Top 10" of a certain variable (namely 'lastrp' in this case). I don't even know where to start, nothing I have tried works...
Any suggestions or snippets? Here is the code:
<?php
$CACHE_PATH = "xml/cache";
$INTERVAL = 3600;
$tmp = "";
$xmldata = "";
$needupdate = FALSE;
$rev = 1;
$is_head = 0;
$GUILD = 0;
class Guild
{
var $name;
var $realm;
var $timestamp;
var $shield;
var $emblem;
var $keepowned;
var $activechar;
var $activemem;
var $rp;
var $chars;
var $lastrp;
var $recent;
var $three;
var $seven;
var $week;
var $inactive;
var $activelvl;
var $totallvl;
var $activerp;
var $classes;
var $classes_act;
var $top;
function Guild($n)
{
$this->name = $n;
$this->realm = "";
$this->timestamp = "";
$this->shield = "";
$this->emblem = "";
$this->keepowned = "";
$this->activechar = 0;
$this->activemem = 0;
$this->rp = 0;
$this->chars = 0;
$this->lastrp = 0;
$this->recent = 0;
$this->three = 0;
$this->seven = 0;
$this->week = 0;
$this->inactive = 0;
$this->activelvl = 0;
$this->totalrp = 0;
$this->totallvl = 0;
$this->activerp = 0;
$this->classes = array();
$this->classes_act = array();
$this->top["level"] = array("", 0);
$this->top["rank"] = array("", 9);
$this->top["lastrp"] = array("", 0);
$this->top["totalrp"] = array("", 0);
$this->top["weaponcraft"] = array("", 0);
$this->top["armorcraft"] = array("", 0);
$this->top["tailoring"] = array("", 0);
$this->top["fletching"] = array("", 0);
}
function getactivechar()
{
return ($this->chars - $this->inactive);
}
}
$CHARS = array ();
$CHARS_MAP = array();
$CHAR = 0;
class Char
{
var $name;
var $laston;
var $nlaston;
var $race;
var $class;
var $level;
var $rank;
var $totalrp;
var $lastrp;
var $anon;
var $weaponcraft;
var $armorcraft;
var $fletching;
var $tailoring;
function Char($n)
{
$this->name = $n;
$this->laston = "Inactive";
$this->nlaston = "0";
$this->race = "";
$this->class = "";
$this->level = 0;
$this->rank = 9;
$this->totalrp = 0;
$this->lastrp = 0;
$this->anon = "TRUE";
$this->weaponcraft = 1;
$this->armorcraft = 1;
$this->fletching = 1;
$this->tailoring = 1;
}
}
function characterData($parser, $data)
{
global $tmp;
$tmp = $data;
}
function startElement($parser, $tagName, $attrs)
{
global $GUILD, $CHAR;
if ($tagName == "GUILD")
{
while (list ($key, $val) = each ($attrs))
{
switch ($key)
{
case "NAME":
$GUILD = new Guild($val);
break;
case "REALM":
$GUILD->realm = $val;
break;
case "KEEPOWNED":
$GUILD->keepowned = $val;
break;
case "ACTIVECHARS":
$GUILD->activechar = $val;
break;
case "ACTIVEMEMBERS":
$GUILD->activemem = $val;
break;
case "GUILDRP":
$GUILD->rp = $val;
break;
case "TIMESTAMP":
$GUILD->timestamp = $val;
break;
}
}
}
elseif ($tagName == "CHARACTER")
{
while (list ($key, $val) = each ($attrs))
{
switch ($key)
{
case "NAME":
$CHAR = new Char($val);
break;
case "LASTON":
$CHAR->laston = ereg_replace("_", " ", $val);
switch ($CHAR->laston)
{
case "Recently":
$GUILD->recent++;
$CHAR->nlaston = 4;
break;
case "3 Days":
$GUILD->three++;
$CHAR->nlaston = 3;
break;
case "7 Days":
$GUILD->seven++;
$CHAR->nlaston = 2;
break;
case "2 Weeks":
$GUILD->week++;
$CHAR->nlaston = 1;
break;
default:
$GUILD->inactive++;
$CHAR->nlaston = 0;
break;
}
break;
}
}
}
}
function endElement($parser, $tagName)
{
global $GUILD, $CHARS, $CHAR, $tmp;
global $CHARS_MAP;
if ($tagName == "CHARACTER")
{
if ($CHAR)
{
array_push($CHARS, $CHAR);
if (preg_match("/([A-z]+) ([A-z]+)/", $CHAR->name, $match))
{
$CHAR->name = $match[1];
}
$CHARS_MAP[$CHAR->name] =& $CHARS[count($CHARS) - 1];
$GUILD->chars++;
$CHAR = 0;
}
}
else
{
switch($tagName)
{
case "RACE":
$CHAR->race = $tmp;
break;
case "CLASS":
$CHAR->class = $tmp;
if (isset($GUILD->classes[$tmp]))
{
$GUILD->classes[$tmp]++;
}
else
{
$GUILD->classes[$tmp] = 1;
}
if (isset($GUILD->classes_act[$tmp]))
{
if ($CHAR->laston != "Inactive")
{
$GUILD->classes_act[$tmp]++;
}
}
else
{
if ($CHAR->laston != "Inactive")
{
$GUILD->classes_act[$tmp] = 1;
}
else
{
$GUILD->classes_act[$tmp] = 0;
}
}
break;
case "LEVEL":
$CHAR->level = $tmp;
$GUILD->totallvl += $tmp;
if ($CHAR->laston != "Inactive")
{
$GUILD->activelvl += $CHAR->level;
}
break;
case "GUILDRANK":
$CHAR->rank = $tmp;
break;
case "TOTALRP":
$CHAR->totalrp = $tmp;
$GUILD->totalrp += $CHAR->totalrp;
if ($CHAR->laston != "Inactive")
{
$GUILD->activerp += $CHAR->totalrp;
}
break;
case "LASTWEEKRP":
$CHAR->lastrp = $tmp;
$GUILD->lastrp += $CHAR->lastrp;
break;
case "ANON":
$CHAR->anon = $tmp;
break;
case "WEAPONCRAFT":
$CHAR->weaponcraft = $tmp;
break;
case "ARMORCRAFT":
$CHAR->armorcraft = $tmp;
break;
case "FLETCHING":
$CHAR->fletching = $tmp;
break;
case "TAILORING":
$CHAR->tailoring = $tmp;
break;
}
}
}
function getguilddata()
{
global $cache_path, $server, $guildid;
global $xmldata, $INTERVAL, $needupdate;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$filetime = filemtime("xml/cache/test_16.xml");
if ((!$filetime) || ((time() - $filetime) > $INTERVAL))
{
$fp = fopen("test.xml","r");
if (!$fp)
{
$fp = fopen("xml/cache/Bedevere_16.xml", "r") or Error("cannot read data file.");
}
$needupdate = TRUE;
}
else
{
$fp = fopen("xml/cache/Bedevere_16.xml", "r") or Error("cannot read data file.");
}
while ($data = fgets($fp, 1024))
{
$xmldata .= $data;
if (!xml_parse($xml_parser, $data, feof($fp)) )
{
Error(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
fclose($fp);
}
function print_header()
{
global $is_head;
if ($is_head)
return;
echo <<<EOT
EOT;
$is_head = 1;
}
function print_chardef_tr($char)
{
global $GUILD;
$lastrp = number_format($char->lastrp);
$totalrp = number_format($char->totalrp);
if ($char->laston != "Inactive")
{
print "\t\t<tr class=\"first\">\n";
}
else
{
print "\t\t<tr class=\"inactive\">\n";
}
echo <<<EOT
<td>$char->name</td>
<td>$char->race</td>
<td>$char->class</td>
<td>$char->level</td>
<td>$char->lastrp</td></tr>
EOT;
}
function print_guild()
{
global $SCRIPT_NAME;
global $GUILD, $server, $guildid;
global $EMBLEM, $img_path, $pri_color;
$t = basename($GUILD->shield);
list($pri, $sec, $pat) = sscanf($t, "%d-%d-%d-full.png");
$t = basename($GUILD->emblem);
$guildrp = number_format($GUILD->rp);
$activelvl = (integer)($GUILD->activelvl / $GUILD->getactivechar());
$totallvl = (integer)($GUILD->totallvl / $GUILD->chars);
$activerp = number_format((integer)($GUILD->activerp / $GUILD->getactivechar()));
$totalrp = number_format((integer)($GUILD->totalrp / $GUILD->chars));
}
function print_chars_default()
{
global $CHARS;
global $SCRIPT_NAME, $sortby;
global $server, $guildid;
echo <<<EOT
<table cellspacing="1" align="center">
<tr class="content-title">
<td>Name</td>
<td>Race</td>
<td>Class</td>
<td>LV</td>
<td>RPWeek</td>
</tr>
EOT;
foreach($CHARS as $char)
{
print_chardef_tr($char);
}
echo <<<EOT
</table>
EOT;
}
getguilddata();
print_header();
print_guild();
print_chars_default();
?>[/SIZE]