rbrown Posted January 4, 2013 Share Posted January 4, 2013 trying this: foreach ($xml->nation as $nation) { echo "nation:".$nation['name']."<hr>"; foreach ($xml->nation->region as $region) { echo $region['name'].'<br>'; } } With this: <regions> <nation url="canada" name="Canada"> <region url="abbotsford" name="Abbotsford"/> <region url="barrie" name="Barrie"/> etc... </nation> <nation url="uk" name="United Kingdom"> <region url="aberdeen" name="Aberdeen"/> <region url="barnsley" name="Barnsley"/> etc... </nation> etc... </regions> What I get is all the same regions in the first nation element. =============== Canada abbotsford barrie etc... United Kingdom abbotsford barrie etc... =============== Should be... =============== Canada abbotsford barrie etc... United Kingdom Aberdeen Barnsley etc... =============== Need to change the second foreach to pull the regions under nation. Tried setting it like this: $xml->$nation->region as $region $xml->$nation['name']->region as $region $xml->nation[name]->region as $region $nation2 = $nation['name']; $xml->$nation2->region as $region $xml->{$nation2}->region as $region $xml->{'$nation2'}->region as $region $xml->nation[]->region as $region $xml->nation()->region as $region And tried a few other things... Can't hit the right combination and I know I know what to do, but I can't seem to pull it out of my brain right now... It has been a while since I played with xml. Searched google and found somethings but the layout of the XML was different or they were using id's in the xml to index the loop. and when I tried the child loops couldn't get it to output the value only the url and name. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/ Share on other sites More sharing options...
Jessica Posted January 4, 2013 Share Posted January 4, 2013 foreach ($xml->nation as $nation) { echo "nation:".$nation['name']."<hr>"; foreach ($xml->nation->region as $region) { While you define $nation, you don't use it in your next foreach. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403123 Share on other sites More sharing options...
rbrown Posted January 4, 2013 Author Share Posted January 4, 2013 I did... " Tried setting it like this: $xml->$nation->region as $region $xml->$nation['name']->region as $region $xml->nation[name]->region as $region $nation2 = $nation['name']; $xml->$nation2->region as $region $xml->{$nation2}->region as $region $xml->{'$nation2'}->region as $region $xml->nation[]->region as $region $xml->nation()->region as $region " Just left out all the rest of the code so you could see the different ways I tried setting the second foreach. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403127 Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 You don't need $xml at that point, just $nation. Exact same way you can do a foreach($array as $value) and only need to use $value. foreach ($nation->region as $region) { Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403129 Share on other sites More sharing options...
rbrown Posted January 4, 2013 Author Share Posted January 4, 2013 Beauty... it works... Also while I was waiting tried this and this works too... $i = 0; foreach ($xml->nation as $nation) { echo "<hr>".$nation['name']."<hr>"; foreach ($xml->nation[$i]->region as $region) { //echo'url: '.$region['url'].'<br>'; echo $region['name'].'<br>'; } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403130 Share on other sites More sharing options...
Jessica Posted January 4, 2013 Share Posted January 4, 2013 *headdesk* there are so many things wrong with that. Please go read the manual on foreach. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403135 Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 *headdesk* there are so many things wrong with that. Please go read the manual on foreach. It wasn't even $i=>$nation either Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403152 Share on other sites More sharing options...
rbrown Posted January 4, 2013 Author Share Posted January 4, 2013 Jessica, what do you mean by that exactly? I have noticed from reading a lot of posts that you "answer" that you seem to be more condescending than helpful. I thought the point of the forums is to help people not belittle them. Granted there are people who ask for help without trying to figure it out first. Also if you are going to answer, at least read the whole post first and try to understand it before posting. Case in point, I had listed some of what I had tried in the second foreach. If you know everything, then spread the knowledge. And explain why you should do it this way rather than another way. If not, then stop running up you post count. Or could it be, you actually have hit your head on the desk too many times? Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403208 Share on other sites More sharing options...
Jessica Posted January 4, 2013 Share Posted January 4, 2013 Jessica, what do you mean by that exactly? I have noticed from reading a lot of posts that you "answer" that you seem to be more condescending than helpful. I thought the point of the forums is to help people not belittle them. Granted there are people who ask for help without trying to figure it out first. Also if you are going to answer, at least read the whole post first and try to understand it before posting. Case in point, I had listed some of what I had tried in the second foreach. If you know everything, then spread the knowledge. And explain why you should do it this way rather than another way. If not, then stop running up you post count. Or could it be, you actually have hit your head on the desk too many times? I mean that there are a lot of things wrong with the way you did it. That's a simple statement. Me telling you to go read the manual means... I think you should go read the manual. For one, foreach can be written as foreach($arr AS $key=>$val){} which is a lot simpler than defining an iterator, and incrementing it after each loop. Did you see that part of the manual? If you wanted to reference the original array instead of $nation you could at least use the built in functionality! All of the things you listed that you tried were wrong. Irrelevant to my post. Requinix had already posted the correct way so there's no point in me re-stating it. I never claimed to know everything, so your comments are also irrelevant. I simply don't care whether people think I'm nice or not on this forum, so I don't bend over backwards to kiss the ass of the person asking me (and everyone else) for help. If you're offended that I think you messed up, get used to it. I didn't insult you, I stated the fact that there were a lot of things wrong with it. Read my signature if you don't understand my posts. It explains why I post what I do. If you don't like it, you can ignore my replies, just like I ignore a few people who I don't like. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403212 Share on other sites More sharing options...
rbrown Posted January 4, 2013 Author Share Posted January 4, 2013 One thing isn't a lot of things... "All of the things you listed that you tried were wrong. Irrelevant to my post. Requinix had already posted the correct way so there's no point in me re-stating it." If there was no point in re-stating it, and since you didn't bother to fully read the post to begin with, then why butt in? And since he gave me the correct answer he could have said don't do it that way because... This forum has changed a quite a bit since I started using it... So I'll be looking for a new home to ask questions and help other people... Have fun here... with your attitude... eventually you'll be by yourself. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403223 Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 We tried. We really did. She told you to use $nation, I told you to use $nation. I even gave you something you could have just copied and pasted. But then you turned around and showed us this weird code that works, sure, but really isn't that great. Certainly more complicated than it needs to be. It's great that you figured out a solution but it's offensive to us when you ignore what we say. Quote Link to comment https://forums.phpfreaks.com/topic/272674-using-a-variable-in-a-simplexml-foreach-loop/#findComment-1403236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.