Jump to content

Using a variable in a simplexml foreach loop


rbrown

Recommended Posts

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...

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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++;

}

Link to comment
Share on other sites

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?

  

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.