Page 1 of 1
XML and PHP - Find Frank and Change his Password! [solved]
Posted: Tue Jun 09, 2009 10:54 pm
by mobone
I don't know why this is so hard for me. All I want to do is a find a user, and change one of his account values.
Keep in mind I don't know which node, user[0], the user is in.
Find frank and change his password.
Code: Select all
<?xml version="1.0"?>
<members>
<user>
<name>frank</name>
<password>Any</password>
</user>
</members>
-----------------------------
Solution:
Code: Select all
$matches = $xml->xpath('/members/user[name="frank"]');
$matches[0]->password = 'Whatever you want the new password to be';
Re: XML and PHP - Find Frank and Change his Password!
Posted: Tue Jun 09, 2009 11:25 pm
by requinix
Do you have PHP 5?
Re: XML and PHP - Find Frank and Change his Password!
Posted: Tue Jun 09, 2009 11:30 pm
by mobone
tasairis wrote:Do you have PHP 5?
Yes
Re: XML and PHP - Find Frank and Change his Password!
Posted: Tue Jun 09, 2009 11:54 pm
by requinix
Good: you can use
SimpleXML:
Code: Select all
<?php
$xml = <<<XML
<?xml version="1.0"?>
<members>
<user>
<name>frank</name>
<password>Any</password>
</user>
</members>
XML;
$simplexml = simplexml_load_string($xml);
echo "User's name is ", $simplexml->user->name, " and his new password is '", $simplexml->user->password, "'";
Re: XML and PHP - Find Frank and Change his Password!
Posted: Wed Jun 10, 2009 12:04 am
by mobone
But what about if I'm trying to find the user, frank in the midst of other users, without using the user[0] identifier. And I also need to know how to set the password to something else, not just print out the current one. But I think if I have the first question, I can answer the second.
Code: Select all
<?xml version="1.0"?>
<members>
<user>
<name>apple</name>
<password>Any</password>
</user>
<user>
<name>tree</name>
<password>Any</password>
</user>
<user>
<name>frank</name>
<password>Any</password>
</user>
</members>
Re: XML and PHP - Find Frank and Change his Password!
Posted: Wed Jun 10, 2009 12:51 am
by requinix
You're the only one who knows how to change the password.
But to loop through everything you can use foreach, just like you would for an array.
Code: Select all
foreach ($simplexml->user as $u) {
echo $u->name, "'s new password is '", $u->password, "'<br>";
}