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>
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
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
[code=php]// EXAMPLE:
$par = 1;
$id = 101;
[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>’;
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. ?