By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,605 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,605 IT Pros & Developers. It's quick & easy.

Can't get this small piece of code to work? Need help

P: n/a
SM
I'm using simpleXML in PHP and i Can't get this small piece of code to
work? Need help

I have an XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<VIDEO>
<item>
<title>Letters</title>
<videos>
<title>a</title>
<title>b</title>
<title>c</title>
</videos>
</item>

<item>
<title>Numbers</title>
<videos>
<title>1</title>
<title>2</title>
<title>3</title>
</videos>
</item>
</VIDEO>

$file = 'db.xml';
$xml = @simplexml_load_file($file);

foreach($xml->item as $item) {
echo 'Letters ' . $item->title;

foreach($item->videos as $vid) {
echo $vid->title;
}
}
Output: LettersaNumbers1
It should be: LettersabcNumbers123

What's wrong?

Thanks
Marco
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
SM wrote:
I'm using simpleXML in PHP and i Can't get this small piece of code to
work? Need help

I have an XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<VIDEO>
<item>
<title>Letters</title>
<videos>
<title>a</title>
<title>b</title>
<title>c</title>
</videos>
</item>

<item>
<title>Numbers</title>
<videos>
<title>1</title>
<title>2</title>
<title>3</title>
</videos>
</item>
</VIDEO>

$file = 'db.xml';
$xml = @simplexml_load_file($file);

foreach($xml->item as $item) {
echo 'Letters ' . $item->title;

foreach($item->videos as $vid) {
echo $vid->title;
}
}
Output: LettersaNumbers1
It should be: LettersabcNumbers123

What's wrong?
You have only 1 videos elementper item, but several title elements in 1
video. Casting $vid->title to string will by default take the text
content of the first title element.

foreach($xml->item as $item) {
echo $item->title;
foreach($item->videos as $vid) {
foreach($vid->title as $title) echo $title;
}
}
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #2

P: n/a
SM
On Jun 3, 9:03 am, Rik Wasmus <luiheidsgoe...@hotmail.comwrote:
SM wrote:
I'm using simpleXML in PHP and i Can't get this small piece of code to
work? Need help
I have an XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<VIDEO>
<item>
<title>Letters</title>
<videos>
<title>a</title>
<title>b</title>
<title>c</title>
</videos>
</item>
<item>
<title>Numbers</title>
<videos>
<title>1</title>
<title>2</title>
<title>3</title>
</videos>
</item>
</VIDEO>
$file = 'db.xml';
$xml = @simplexml_load_file($file);
foreach($xml->item as $item) {
echo 'Letters ' . $item->title;
foreach($item->videos as $vid) {
echo $vid->title;
}
}
Output: LettersaNumbers1
It should be: LettersabcNumbers123
What's wrong?

You have only 1 videos elementper item, but several title elements in 1
video. Casting $vid->title to string will by default take the text
content of the first title element.

foreach($xml->item as $item) {
echo $item->title;
foreach($item->videos as $vid) {
foreach($vid->title as $title) echo $title;
}}

--
Rik Wasmus
...spamrun finished
It works, thanks again
Marco
Jun 27 '08 #3

P: n/a
SM
On Jun 3, 9:03 am, Rik Wasmus <luiheidsgoe...@hotmail.comwrote:
SM wrote:
I'm using simpleXML in PHP and i Can't get this small piece of code to
work? Need help
I have an XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<VIDEO>
<item>
<title>Letters</title>
<videos>
<title>a</title>
<title>b</title>
<title>c</title>
</videos>
</item>
<item>
<title>Numbers</title>
<videos>
<title>1</title>
<title>2</title>
<title>3</title>
</videos>
</item>
</VIDEO>
$file = 'db.xml';
$xml = @simplexml_load_file($file);
foreach($xml->item as $item) {
echo 'Letters ' . $item->title;
foreach($item->videos as $vid) {
echo $vid->title;
}
}
Output: LettersaNumbers1
It should be: LettersabcNumbers123
What's wrong?

You have only 1 videos elementper item, but several title elements in 1
video. Casting $vid->title to string will by default take the text
content of the first title element.

foreach($xml->item as $item) {
echo $item->title;
foreach($item->videos as $vid) {
foreach($vid->title as $title) echo $title;
}}

--
Rik Wasmus
...spamrun finished
I've changed the XML so i only use two 'foreach' loops instead of 3 (I
think it's simply too much)
<VIDEO>
<item>
<title>Letters</title>
<video>a</video>
<video>b</video>
<video>c</video>
</item>

<item>
<title>Numbers</title>
<video>1</video>
<video>2</video>
<video>3</video>
</item>
</VIDEO>
foreach($xml->item as $item) {
echo $item->title;

foreach($item->videos as $vid) {
echo $vid;
}

}

Thanks again for the tip
Marco
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.