Click to See Complete Forum and Search --> : simplexml problems :S


katten
01-30-2006, 12:35 PM
yea well i got a problem with my simple xml script :P
its ment to change the name of the account and the player in the game of freinds made ill post it to show

<?php
include("../includes/config.php");
if(isset($_REQUEST["form"])){
// geting all the values
$oldname = $_REQUEST["old_name"];
$newname = $_REQUEST["new_name"];
$player_file = $folderplayer . $oldname . ".xml";

if(file_exists($player_file)){
$xml = simplexml_load_string(file_get_contents($player_file));
$account = $xml['account'];
$xml['name'] = $newname;



$account_file = $folderaccount . $account . ".xml";
$a_xml = simplexml_load_string(file_get_contents($account_file)); // again, use file_get_contents :p
foreach($a_xml->characters->character as $k => $v) { // foreach loops trough an array, saving the array key in $k and its value in $v
if($a_xml->characters->character[$k]['name'] == $oldname) { // checks if this is the character we want to change
$a_xml->characters->character[$k]['name'] = $newname;
break; // quite loop since we done what we wanted; looping when not needed is <<EVUL>>
}
}
file_put_contents($player_file, $xml->asXML());
file_put_contents($account_file, $a_xml->asXML());
rename("$folderplayer/$oldname.xml", "$folderplayer/$newname.xml");
} else {
print("The player dose not exists");
}
} else if(!isset($_REQUEST["form"])){

echo 'Name lock function<br />
<form action="rename.php" method="POST">
<table>
<tr>
<td>Old name: </td><td><input name="old_name" type="text" value=""></td>
</tr>
<tr>
<td>New name: </td><td><input name="new_name" type="text" value=""></td>
</tr>
</table>
<br>
<input type="Submit" value="Submit" name="form">
<input type="Reset" value="Clear">
</form>';

}
?>

account xml

<?xml version="1.0"?>
<account pass="beta" access="1" type="1" premDays="15">
<characters>
<character name="Hurz"/>
<character name="Hurzel"/>
<character name="Hurzine"/>
</characters>
</account>

files get created in the same place as rename, and they are file not xml files replacing the old once :S

Scleppel
01-30-2006, 12:45 PM
Remove all the single quotes from around variables:
//this
$xml['name'] = '$newname';
//should be
$xml['name'] = $newname;

Variables are not replaced in sinlge quoted strings.

katten
01-30-2006, 01:44 PM
stilll no change :S anyone got a idea :P? uppdated main script

Scleppel
01-30-2006, 01:48 PM
All of them:
file_put_contents('$player_file', $xml->asXML());
file_put_contents('$account_file', $a_xml->asXML());

// should be

file_put_contents($player_file, $xml->asXML());
file_put_contents($account_file, $a_xml->asXML());

katten
01-30-2006, 02:05 PM
okej now the name changed in the player.xml file but not in the account.xml ill post the account.xml