Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
parse xml child/parent help
Old 07-05-2010, 04:46 PM parse xml child/parent help
Junior Talker

Posts: 2
Trades: 0
Hi Guys,

I have been trying to build a child/parent navigation for a little while now using PHP (I'm from a .net background) and I can't get close to the desired results. I am loading my data from an xml file using simplexml successfully but I am trying to workout how I can map that into arrays or variables so I can write it out.

The xml is
Code:
  <Categories>
  <Category>
    <ID>1</ID>
    <Title>Days</Title>
    <Description />
    <ParentID />
    <Meta />
  </Category>
  <Category>
    <ID>2</ID>
    <Title>Monday</Title>
    <Description />
    <ParentID>1</ParentID>
    <Meta />
  </Category>
  <Category>
    <ID>3</ID>
    <Title>Tuesday</Title>
    <Description />
    <ParentID>1</ParentID>
    <Meta />
  </Category>
  <Category>
    <ID>4</ID>
    <Title>Wednesday</Title>
    <Description />
    <ParentID>1</ParentID>
    <Meta />
  </Category>
  <Category>
    <ID>5</ID>
    <Title>Thursday</Title>
    <Description />
    <ParentID>1</ParentID>
    <Meta />
  </Category>
  <Category>
    <ID>6</ID>
    <Title>Friday</Title>
    <Description />
    <ParentID>1</ParentID>
    <Meta />
  </Category>
  <Category>
    <ID>7</ID>
    <Title>Saturday</Title>
    <Description />
    <ParentID/>
    <Meta />
  </Category>
  <Category>
    <ID>8</ID>
    <Title>Sunday</Title>
    <Description />
    <ParentID/>
    <Meta />
  </Category>
</Categories>
Now my PHP
PHP Code:
foreach($categories as $category) {

                if (
$category->ParentID != "")
                {
                    echo 
"<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
                    echo 
"<ul>";
                    foreach(
$categories as $subcategory) {
                    if (
$subcategory->ParentID == $category->ID)
                    {
                        echo 
"<li><a href=index.php?Cat=$subcategory->ID>$subcategory->Title</a></li>";
                    }
                    }
                    echo 
"</ul>";
                }
                else
                {
                    echo 
"<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
                } 
            } 
            echo 
"</ul>"
So my desired output would be something like this:
HTML Code:
<ul id="p7menubar">
<li><a class="trigger" href="#">Days</a>
<ul>
<li><a href="#">Monday</a></li>
<li><a href="#">Tuesday</a></li>
<li><a href="#">Wednesday</a></li>
<li><a href="#">Thursday</a></li>
<li><a href="#">Friday</a></li>
</ul>
</li>
<li><a href="index.htm">Saturday</a></li>
<li><a href="index.htm">Sunday</a></li>
</ul>

If anyone could help me out it would be very much appreciated

Thanks

Al
Muller2 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-06-2010, 03:48 AM Re: parse xml child/parent help
Junior Talker

Posts: 2
Trades: 0
For anyone wanting an update a solution that works has been posted here: http://www.sitepoint.com/forums/show....php?p=4638759
Muller2 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to parse xml child/parent help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.09940 seconds with 12 queries