473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 1625
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
1
by: Nobody | last post by:
I'm trying to write a class where I need to sort a list of TreeNode objects in a certain way. So I thought I'd use: List<ListNodeData> and then use the Sort method. I have the code working...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
48
by: Ward Bekker | last post by:
Hi, I'm wondering if the GC.Collect method really collects all objects possible objects? Or is this still a "smart" process sometimes keeping objects alive even if they can be garbage collected?...
0
by: SM | last post by:
Im using simpleXML in PHP and i can't get this small piece of code to work? Need help I have a XML that looks like this: <?xml version="1.0" encoding="utf-8"?> <VIDEO> <item>...
0
by: service0043 | last post by:
Some bargain prices can be found on jewelry that is sold in bulk and at auction houses on the internet. While the condition of the items might be used, some fine jewelry pieces will be sold as new....
10
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.