squariegoes Posted April 8, 2009 Share Posted April 8, 2009 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? Link to comment https://forums.phpfreaks.com/topic/153221-simplexml-how-to-insert-varaible-into-queries/ Share on other sites More sharing options...
squariegoes Posted April 8, 2009 Author Share Posted April 8, 2009 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? Link to comment https://forums.phpfreaks.com/topic/153221-simplexml-how-to-insert-varaible-into-queries/#findComment-804895 Share on other sites More sharing options...
squariegoes Posted April 8, 2009 Author Share Posted April 8, 2009 Fixed with this - $xt = "book"; $xno = 0; foreach($xml->{$xt}[$xno]->attributes() as $a => $b) { echo $a,'="',$b,"\"\n"; } Link to comment https://forums.phpfreaks.com/topic/153221-simplexml-how-to-insert-varaible-into-queries/#findComment-804937 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Excellent, please mark as [sOLVED] so everyone knows. Thanks! Link to comment https://forums.phpfreaks.com/topic/153221-simplexml-how-to-insert-varaible-into-queries/#findComment-805090 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.