/    Sign up×
Community /Pin to ProfileBookmark

Find element in XML loop

Hello all!
I’m stuck here with a rather complicated (for me at least) problem.
Let me explain.
I’m using XML for my website map. The structure inside looks like this:

[CODE]<it id=”1″ name=”Home”>
<it id=”101″ name=”Useful Links” />
</it>

<it id=”2″ name=”Share”>
<it id=”201″ name=”Programmes”/>
<it id=”202″ name=”Forums”>
<it id=”207″ name=”num 1″/>
<it id=”208″ name=”num 2″ display=”none”/>
</it>
</it>[/CODE]

Two main attributes are used: ID and NAME

Now, what I want to do is return a user-friendly path to the current page. My current page id is stored in “[B]$id[/B]“.

Only thing is I don’t know how many parents I have, meaning I need to loop backwards.

This was a script I tried (using [B]simpleXML[/B]):

[code=php]// EXAMPLE:
$par = 1;
$id = 101;[/code]

[code=php]$f = false;
foreach($sitemap->children() as $child)
{
foreach($child->attributes() as $a=>$b)
{
if($a == “id”){
if($b == $par){
$f = true;
}else{
$f = false;
}
}
if($a == “name”){
if($f){
$parent = $b;
}
}
}

foreach($child->children() as $sub)
{
foreach($sub->attributes() as $a=>$b)
{
if($a == “id”){
if($b == $id){
$f = true;
}else{
$f = false;
}
}
if($a == “name”){
if($f){
$childa = $b;
}
}
}
}
}

echo ‘<address id=”location”>Current page : ‘;
if(isset($childa)){
echo ‘<a href=”index.php?id=’.$par.'”>’.$parent.'</a> > <strong>’.$childa.'</strong>’;
}else{
echo ‘<strong>’.$parent.'</strong>’;
}
echo ‘</address>’;[/code]

Its not very efficient and enables me to have only two levels. Yet I dont know how much there will be, and anyway, its useless repeating.

How on earth should I do that? Is there a way to “search” an xml object? Or should I look through every level, keep values in a variable, and when I find the current ID, echo the parents?
I’m sure its pretty simple, its just that my head is imploding. ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 26.2009 — Can you alter the XML, adding a "parent" attribute to the <it> tags with the value being the id of the parent?
Copy linkTweet thisAlerts:
@rwtauthorAug 27.2009 — mmh, that would be a real bother (lots of php lines to change) and wouldn't that misuse the power of xml!??
Copy linkTweet thisAlerts:
@TeufelAug 27.2009 — Do you know XPATH?

It works like this:
[CODE]
$bla = simplexml_load_string($foo);
$bla->xpath("//element[@attribute='attribute value' or @attribute2 = 'attribute2 value' or contains(@attribute3, 'something within attribute3')]");
[/CODE]

Then you get back an array of some sort (not sure anymore), but it's really handy and nice.
×

Success!

Help @rwt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...