Jump to content

SimpleXML - how to insert varaible into queries????


squariegoes

Recommended Posts

I'm trying to query a simplexml object to obtain attribute names and values. The following works -

 

foreach($xml->book[0]->attributes() as $a => $b) {

  echo $a,'="',$b,"\"\n";

}

 

 

but i want to make this query work dynamically and replace book with a variable but when I do I get the following error -

 

Warning: main() [function.main]: Node no longer exists in /home/ . . . .

 

My code works when I substitute the 0 after book with a variable which has had intval() used on it but not when I substitute book for a variable.

 

$xno = intval(0);

foreach($xml->book[$xno]->attributes() as $a => $b) {

  echo $a,'="',$b,"\"\n";

}

 

Can anyone help me out?

Here's the code that's not working -

$xt = strval("book");

$xno = intval(0);

 

foreach($xml->$xt[$xno]->attributes() as $a => $b) {

  echo $a,'="',$b,"\"\n";

}

 

 

I've tried all sorts of thing to get the string variable to run in the query including using strval, not using it, using it with quotes and without quotes. I am really stuck on this. Is it even right to use strval()? Or is there another method to get the variable to execute?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.