AS programmer needs help with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
antizamm
Forum Newbie
Posts: 1
Joined: Mon Feb 08, 2010 6:02 am

AS programmer needs help with php

Post by antizamm »

Hello!I don't know ANYTHING about PHP :) ,so I was wandering if somebody can help me.I have online game(AS 3.0) and I want to save score online to xml file.AS would send two variables and php would get them and add a new node to xml,something like this:

Code: Select all

 
<player>
<name>//first var</name>
<score>//second var</score>
</player>
 
So this is my PHP script:

Code: Select all

 
<?php
    $phpvar1 = $_POST['first'];
    $phpvar2 = $_POST['second'];
 
    $file ="scores.xml";
    $fp = fopen($file, "rwx") or die("cannot open file");
    $str = fread($fp, filesize($file));
 
    $xml = new DOMDocument();
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->loadXML($str) or die("Error"); 
    
    echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";
    
    $root   = $xml->documentElement;
    $fnode  = $root;
 
    $ori    = $fnode->childNodes->item(0);
 
    $name= $xml->createElement("name");
    $nameText = $xml->createTextNode($phpvar2);
    $name->appendChild($nameText);
 
    $result=$xml->createElement("result");
    $resultText = $xml->createTextNode($phpvar1);
    $result->appendChild($resultText);
 
    $player  = $xml->createElement("player");
    $player->appendChild($name);
    $player->appendChild($result);
    $fnode->insertBefore($player,$ori);
    echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
 
     $XML->save('scores.xml');
?>
 
I just want to know is this code ok?Does it do exactly what I have described?
Because if this is ok, then I would know that I'm posting wrong vars from Actionscript...
Thanks...
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: AS programmer needs help with php

Post by tr0gd0rr »

The last line has uppercase `$XML` instead of lowercase. Could that be the problem? Do you get any errors?

Have you thought about building up an xml string instead of using DOMDocument? That would be much simpler in this case.
Post Reply