This is a discussion on I have simple question using simpleXML and PHP within the PHP Language forums, part of the PHP Programming Forums category; Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello
I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD> <title>Moonlight</title> <year>1978</year> <company>RCA</company> <item label="CD 1:"> <title>Moonlight</title> <title>Take It</title> <title>My Forever</title> <title>Anymore</title> </item> <item label="Cd 2:"> <title>All Right</title> <title>No More</title> <title>Between</title> <title>Why</title> </item> </CD> </discography> Note that there could be more then one <item> tag. In this case, there is 2, but it could be 1 or 5... Using php, the output should look like this: Moonlight 1978 RCA CD 1: *Moonlight *Take It *My Forever *Anymore CD 2: *All Right *No More *Between *Why I'm almost there, but i don't know how to loop to get the <title>'s of al the <item> tags and the attribute of all the <item>'s tag. Here's my code: <?php if (file_exists('db/discography/cd_1.xml')) { $xml = simplexml_load_file('db/discography/cd_1.xml'); } else { exit('Error opening the XML file.'); } $title = $xml->CD->title; $year = $xml->CD->year; $company = $xml->CD->company; ?> <h1><?php echo $title; ?></h1> <h3><?php echo $year; ?></h3> <h3><?php echo $company; ?></h3> //get the <title>'s of all the <item> <ul> <?php foreach ($xml->CD->item as $item) { ?> <li><?php echo $item->title; ?></li> <?php } ?> </ul> My code will output: Moonlight 1978 RCA *Moonlight How do i loop through all the <title> in al lthe <item>'s that exist in the xml + how do i get the attribute of the <item> file?? Help! Thanks Marco |
|
|||
|
SM escribió/wrote:
[...] > //get the <title>'s of all the <item> > <ul> > <?php > foreach ($xml->CD->item as $item) { > ?> > <li><?php echo $item->title; ?></li> > <?php > } > ?> > </ul> [...] > How do i loop through all the <title> in al lthe <item>'s that exist > in the xml + how do i get the attribute of the <item> file?? It's very easy if you know the actual contents of the variables you handle. Add this line to the loop and you'll understand the issue: var_dump($item); -- -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programación web: http://bits.demogracia.com -- Mi web de humor al baño María: http://www.demogracia.com -- |
|
|||
|
SM schreef:
> How do i loop through all the <title> in al lthe <item>'s that exist > in the xml + how do i get the attribute of the <item> file?? > <?php foreach ($xml->CD->item as $item) { foreach ($item->title as $value) { ?> <li><?php echo $value; ?></li> <?php } } ?> JW |
|
|||
|
On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote:
> SM schreef: > > > How do i loop through all the <title> in al lthe <item>'s that exist > > in the xml + how do i get the attribute of the <item> file?? > > <?php > foreach ($xml->CD->item as $item) { > foreach ($item->title as $value) { > ?> > <li><?php echo $value; ?></li> > <?php > } > } > ?> > > JW Thanks JW & Alvaro: I've decided on JW solution. I couldn't understand the var_dump solution. I was also able to get the attributes.. Anyways, if it helps, here's the code: Thanks JW & Alvaro: I've decided on JW solution. I couldn't understand the var_dump solution. I was also able to get the attributes.. Anyways, if it helps, here's the final code: .... <h1>Tracks</h1> <?php foreach ($xml->CD->item as $item) { ?> <h3><?php echo $item['label']; ?></h3> <ul> <?php foreach ($item->title() as $track) { ?> <li><?php echo $track; ?></li> <?php } ?> </ul> <?php } ?> .... The output: Moonlight 1978 RCA CD 1: Moonlight Take It My Forever Anymore CD 2: All Right No More Between Why |
|
|||
|
SM wrote:
> On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote: >> SM schreef: >> >>> How do i loop through all the <title> in al lthe <item>'s that exist >>> in the xml + how do i get the attribute of the <item> file?? >> <?php >> foreach ($xml->CD->item as $item) { >> foreach ($item->title as $value) { >> ?> >> <li><?php echo $value; ?></li> >> <?php >> } >> } >> ?> >> >> JW > > Thanks JW & Alvaro: > I've decided on JW solution. I couldn't understand the var_dump > solution. I was also able to get the attributes.. Anyways, if it > helps, here's the code: > <code snipped> Alvaro was trying to help you in a more general way. var_dump() displays the contents of the array, including the indexes and values. From there you have a lot better shot at figuring out how to build the code you need to use the array. Then the next time you run into a problem with arrays or other complex objects (whether simpleXML or something else), you can solve it more easily. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
On May 8, 11:01 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> SM wrote: > > On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote: > >> SM schreef: > > >>> How do i loop through all the <title> in al lthe <item>'s that exist > >>> in the xml + how do i get the attribute of the <item> file?? > >> <?php > >> foreach ($xml->CD->item as $item) { > >> foreach ($item->title as $value) { > >> ?> > >> <li><?php echo $value; ?></li> > >> <?php > >> } > >> } > >> ?> > > >> JW > > > Thanks JW & Alvaro: > > I've decided on JW solution. I couldn't understand the var_dump > > solution. I was also able to get the attributes.. Anyways, if it > > helps, here's the code: > > <code snipped> > > Alvaro was trying to help you in a more general way. var_dump() > displays the contents of the array, including the indexes and values. > From there you have a lot better shot at figuring out how to build the > code you need to use the array. Then the next time you run into a > problem with arrays or other complex objects (whether simpleXML or > something else), you can solve it more easily. > > -- > ================== > Remove the "x" from my email address > Jerry Stuckle > JDS Computer Training Corp. > jstuck...@attglobal.net > ================== Thanks for the explanation. It's a good tip for debugging... Marco |
![]() |
| Thread Tools | |
| Display Modes | |
|
|