TNO
-
Total Posts
:
2094
- Scores: 36
-
Reward points
:
0
- Joined: 12/18/2004
- Location: Earth
-
Status: offline
|
My first PHP, needs criticism
Thursday, November 30, 2006 11:53 PM
( permalink)
Since my current webhost only supports PHP, I've been forced to make a program in it (crying on the inside).... This PHP is designed to update an XML file when the following variables are passed through the URL: .../map.php?map= ####&id= ####&path= ####&x= ####&y= #### Are there any obvious blunders in my code that you could point out?
#Get variables passed to this page
icon=$_GET['id'];
map=$_GET['map'];
path=$_GET['path'];
x=$_GET['x'];
y=$_GET['y'];
# to load xml from a file
$doc = xmldocfile( ".../map.xml" ) or die("Database could not be loaded");
# get root node "guilds"
$guilds = $doc->root();
#create xpath expression to test for existence of an icon with the id tag:
$xp = new domxpath($doc)
$result = $xp->query("/guilds/icon[@id=$icon]")
#If icon of id exists, update information
if($result!=Null){
$result.childNodes[0]->data = $map
$result.childNodes[1]->data = $path
$result.childNodes[2]->data = $x
$result.childNodes[3]->data = $y
}
#else create nodes with information,
else {
//create <icon> node under guilds node
$iconNode = make_node($guilds,"icon","");
//set id attribute
$iconNode->setattr("id",icon);
//create nodes under <icon>
$iconNode->new_child("map",$map)
$iconNode->new_child("path",$path)
$iconNode->new_child("x",$x)
$iconNode->new_child("y",$y)
}
#save doc
$doc->save(".../map.xml");
//////////////////////////
# function for making child nodes
function make_node($parent,$name,$content)
{
# adds a new child node to parent node
$parent->new_child($name,$content);
# return the newly added child as a reference
return $parent->lastchild();
}
Sample XML file: <?xml version="1.0" encoding="ISO-8859-1"?>
<guilds>
<icon id="####">
<map>???</map>
<path>http://blah</path>
<x>100</x>
<y>100</y>
</icon>
<icon id="####">
<map>???</map>
<path>http://blah2</path>
<x>200</x>
<y>200</y>
</icon>
</guilds>
<message edited by TNO on Friday, December 01, 2006 12:05 AM>
To iterate is human, to recurse divine. -- L. Peter Deutsch
|
|
|
|