XML and PHP - Find Frank and Change his Password! [solved]

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
mobone
Forum Newbie
Posts: 8
Joined: Mon May 18, 2009 8:04 pm

XML and PHP - Find Frank and Change his Password! [solved]

Post 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';
 
Last edited by mobone on Wed Jun 10, 2009 7:04 am, edited 2 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML and PHP - Find Frank and Change his Password!

Post by requinix »

Do you have PHP 5?
mobone
Forum Newbie
Posts: 8
Joined: Mon May 18, 2009 8:04 pm

Re: XML and PHP - Find Frank and Change his Password!

Post by mobone »

tasairis wrote:Do you have PHP 5?
Yes
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML and PHP - Find Frank and Change his Password!

Post 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, "'";
mobone
Forum Newbie
Posts: 8
Joined: Mon May 18, 2009 8:04 pm

Re: XML and PHP - Find Frank and Change his Password!

Post 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>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML and PHP - Find Frank and Change his Password!

Post 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>";
}
Post Reply