Author Topic: XML: PHP example from 7 hours Saturday morning  (Read 2821 times)

konoko

  • Minion
  • **
  • Posts: 23
  • Under your Keyboard
    • konoko's ramblings
XML: PHP example from 7 hours Saturday morning
« on: October 04, 2008, 12:33:01 PM »
Good morning! I've been wanting to make a semi-dynamic page on my pseudo-existant website that has my most active characters' avatars as tracked by City Info Tracker. All I really needed was the character name and ID. However in trying to extract the data from the XML file, I got distracted. Actually, I was playing for 3 hours ('bout the only time I have to play is Friday night) and "accidentally" got a badge. Herostats was running so I was curious if cohtitan was update. That reminded me I had started this last month. Typical programmer can't stop once started.

Here is the result, 95% of this was done in the last 7 hours. Takes a moment to process the bubble sort on my 22 characters, should stop messing with the XML object and create indexed arrays of the character ID's and sort that array based on the character ID's updated element. But hey, it works.  Maybe look at using Javascript. AJAX, Ruby on Rails, FORTRAN!

Now that I know how the SimpleXML object works, I can pull the character ID's I need. I'll code something for that after I get some sleep.

note: comments have been haphazardly carriage returned to prevent side scrolling, sorry
Code: (PHP5::CIT Badge XML Feed Parser) [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div>
<p><?php
/**
 * @ALERT
 * This code should only be used on PHP5 for the simplexml object
 */
/**
 * $fileTitan
 * Please update this string with the URL to the XML file
 */
$fileTitan "http://cit.cohtitan.com/xml/priv/LINENOISE";
$fileTitanTest "cohtitantest.xml";//local file for faster testing
/**
 * $xmlCharacters
 * This is a SimpleXMLElement object.
 */
$xmlCharacters simplexml_load_file($fileTitanTest);
/* original code, dumps all data */
//echo $xml->getName() . "<br />";
//
//foreach($xml->children() as $child)
//&#160; {
//&#160;  echo $child->getName() . ": " . $child . "<br />";
//&#160;  foreach($child->children() as $character)
//&#160;  {
//&#160;  echo $character->getName() . ": " . $character . "<br />";
//&#160;  }
//&#160; }
/**
 * $arrXMLCharcterChildren
 * Array may be useful later, each key mathces the name of an element in the cohtitan XML file
 */
$arrXmlCharacterChildren = array(
"char_id" => "000"
"name" => "xxx"
"status" => "xxx"
"security_level" => "xxx"
"archetype_name" => "xxx"
"origin_name" => "xxx"
"server_name" => "xxx"
"primary_name" => "xxx"
"secondary_name" => "xxx"
"global_name" => "xxx"
"game_id" => "0"
"last_update" => "ISO8601"
"main_char" => "0"
"current_project" => "0"
"verified_count" => "000"
"unverified_count" => "000"
"large_avatar" => "xxxxxx"
"medium_avatar" => "xxxxxx"
"small_avatar" => "xxxxxx"
"bio_text" => "xxxxxxxxxxxxxxxxxxxxx");


/**
 * $totalCharacters
 * counts the number of "character" elements in the XML object, assumes XML object came from
 *&#160; cohtitan
 */
$totalCharacters count($xmlCharacters->character);

$onlineCharacter=array("char_id"=>"000");// may be used later

/**
 * corrective action
 * last_update almost complies with ISO8601 formatting, it is missing the timezone.
 * This foreach() loop adds the timezone (Eastern -4) and since it's looping through the whole
 *&#160; thing looks for the onlin character.
 * Could be used to locate other data, such as main or project characters and store their ID's
 *&#160; in a separate array
 */
foreach($xmlCharacters->children() as $character)
{
//convert last_update to ISO8601 time standard string
$character->last_update str_replace(" ","T",$character->last_update) . "-04:00";

if($character->status == "Online")
{
/** This pulls the online character */
$onlineCharacter[char_id]=$character->char_id;
}
}

/** This section calls the functions. Edit as needed */
echo "<hr />\n";//horizontal line
displayLastUpdate($xmlCharacters);// in the current example, display days since update
echo "<hr />\n";//horizontal line
sortByDate($xmlCharacters);// in current example, sort entire XML object by date
echo "<hr />\n";//horizontal line
displayLastUpdate($xmlCharacters);// in curent example, display days since update again, verify sorting worked.

?>

</p>
</div>
</body>
</html>
<?php
function 
displayLastUpdate(SimpleXMLElement &$xml) {
/**
 * displayLastUpdate(SimpleXMLElement object)
 * calculates days since last update and displays results
 */
foreach($xml->children() as $character)
{
echo "<p>\n";
$timeTemp strtotime($character->last_update);//convert ISO8601 time to Unix timestamp
$tmpAge time() - $timeTemp;// how many seconds has it been since last_update
echo "I believe it's been "
intval($tmpAge 60 60 24)
" days since "
$character->name
" was updated. <br />\n";
};
}

function displayDumpData(SimpleXMLElement &$xml) {
/**
 * displayDumpData(SimpleXMLElement object)
 * dump all data in a table in the order it appears in the XML object
 * This assumes 2 levels nesting, can be applied to nearly any XML object with 2 levels, meaning
 *&#160; not cohtitan specific
 */
echo "<table>\n";
foreach($xml->children() as $character)
{
echo "<tr>\n";
foreach($character->children() as $data)
{
echo "<td>\n";
echo $data;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}

function sortByDate(SimpleXMLElement &$xml) {
/**
 * sortByDate(SimpleXMLElement object)
 * moves XML data around based on timestamp
 */
$tmpUpdate1 time();
$tmpUpdate2 time();
$tmpCount count($xml->character);// total elements
$tmpArrayMax $tmpCount 1;//max element index
/*
 * NOTE!
 * SimpleXML will let you specifiy a node that doesn't exist. Somewhat. Be careful.
 */

/* Highly inefficient bubble sort */
for ($i=0$i <= $tmpArrayMax$i++)
{
for ($j=0$j <= $tmpArrayMax$j++)
{
//this comparison can be done several ways, this one works
$tmpUpdate1 strtotime($xml->character[$i]->last_update);
$tmpUpdate2 strtotime($xml->character[$j]->last_update);
if ($tmpUpdate1 $tmpUpdate2){
/*
 * one date is sooner than the other, swapping
 */
swapCharacters($xml,$i,$j);
}
else
{
/*
 * no need to swap
 * do nothing
 */
}
}
}
}

function swapCharacters(SimpleXMLElement &$xmlAgain$firstNode$secondNode)
{
/**
 * swapCharacters(SimpleXMLElement object, integer, integer)
 * swaps data stored in two character nodes from a cohtitan xml dumped to a SimpleXMLElement
 *&#160; object
 * Ideally, one would just swap the data by specifying the entire node
 * In fact, a previous version of this file did just that, but each element manually using the
 *&#160; $arrXmlCharacterChildren array
 */
$tmp;
$element;
foreach($xmlAgain->character[$firstNode]->children() as $data)
{
/*
 * Loops through the elements in the character node and swaps them
 */
$element $data->getName();
$tmp str_replace("","",$xmlAgain->character[$secondNode]->$element);//bloody objects
/*
 * NOTE!!!
 * $tmp = $xmlAgain->character[$secondNode]->$element;//bad PHP, no cookie
 * The two lines above (one included in this comment block) deal with temporarily storing
 *&#160; the data inside the SimpleXML object
 * Unfortunately PHP will allow a variable to change from one type (string) to another
 *&#160; (XML Object).
 * For whatever reason, this object referenced another object, so any changes made to the
 *&#160; other object was mirrored on the first object
 * Becuase of this mirroring, $tmp kept getting changed and no data was swapped, only
 *&#160; duplicated.
 * Without the str_replace trick, the bubble sort that calls this function would replace
 *&#160; all data with one dataset
 * SHORT VERSION: Be careful.
 */
$xmlAgain->character[$secondNode]->$element $xmlAgain->character[$firstNode]->$element;
$xmlAgain->character[$firstNode]->$element $tmp;
}
}


?>
« Last Edit: October 04, 2008, 11:14:27 PM by Steiner »
-Paragonwiki Admin

Voltaic Shock

  • Elite Boss
  • *****
  • Posts: 575
Re: XML: PHP example from 7 hours Saturday morning
« Reply #1 on: October 04, 2008, 04:52:55 PM »
Nice!

I'll have to look at it when I get some time.
@Voltaic Shock[/b]
TN Admin, CIT Co-Creator, CGT Developer, Obsolete
---------------------------

  http://www.motorcyclistsunited.com/  http://www.YourFreeiPhone.com/index.php?ref=5292707

Steiner

  • Elite Boss
  • *****
  • Posts: 1,602
    • Steinerd.com
Re: XML: PHP example from 7 hours Saturday morning
« Reply #2 on: October 04, 2008, 05:45:31 PM »
Might want to look into PHP classes, they make the code much easier to read and faster to load.



We gotta get together on this one Volt, Related Thread.

Try and push these two ideas together.
« Last Edit: October 04, 2008, 11:15:22 PM by Steiner »
~Steinerd