tags:

views:

46

answers:

1

Here is my PHP code

$xml= new SimpleXmlElement($rawxml);
 foreach($xml->children()->children() AS $key){
  $id = $xml->{"id"};
  $name = $xml->{"screen_name"};
  $profimg = $xml->{"profile_image_url"};
  echo "$id, $name, $profimg";
 }
$next = $xml->{"next_link"};
echo "index.php?".$next;

Here is the structure of my xml

<?xml version="1.0" encoding="UTF-8"?>
    <users_list>
        <users type="array">
            <user>
              <id>44444</id>
              <screen_name>Some Name</screen_name>
              <profile_image_url>http://www.website.com/picture.jpg&lt;/profile_image_url&gt;
            </user>
            <user>
              <id>555</id>
              <screen_name>Bob</screen_name>
              <profile_image_url>http://www.website.com/picture2.jpg&lt;/profile_image_url&gt;
            </user>
            <user>
              <id>666666</id>
              <screen_name>Frank</screen_name>
              <profile_image_url>http://www.website.com/picture3.jpg&lt;/profile_image_url&gt;
            </user>
        </users>
        <next_link>44444</next_link>
    </users_list>

Im trying to assign the values of the field to variables and then echo them. Then at the bottom echo the nextlink.

I dont get any errors, but it just shows the first field over and over, and doesnt output the nextlink.

+3  A: 

You’re using $key in the foreach expression but $xml inside the foreach body.

I would also prefer an XPath expression rather than your children of the children of the root way:

foreach ($xml->xpath('/users_list/users/user') as $user) {
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "$id, $name, $profimg";
}
Gumbo
this is returning no errors, but no data as well, just blank.
Patrick
@Patrick: Well, it works for me. Did you try my code in substitution of yours or in addition to yours?
Gumbo
im using as substitution
Patrick
@Patrick: I just put the XML code in `$rawxml = '…';`, created a new SimpleXML object like you did and then put my suggested `foreach` after that and it worked like a charm.
Gumbo