0

I've been trying to develop an xml-driven horizontal menu on my own as a private project..
But as it seems I've been stuck.. Roughly..
XML File (navigation.xml)

<navigation> <menu> <link>url</link> <tag>hover help text</tag> <text>Link text</text> </menu </navigation> 

So, I have a variable storring the old link/text/tag name, and I want to update it through SimpleXML/XPath.. But it doesn't seem to work..
PHP Code:

$load = SimpleXML_load_file('database/navigation.xml'); foreach( $load->xpath("menu[@link=". $link ." and @tag=". $tag ." and @text=". $text ."]") as $item ) { $item->link = $link2; //link2->new link value, $link->old value $item->tag = $tag2; //tag2->new tag value, $tag->old value $item->text = $text2; //text2->new text value, $text->old value } $load->asXML('database/navigation.xml'); 

Is there any way I can make it work? Thanks in advance!

1 Answer 1

1

The "@" symbol in "@link" is used to reference the attribute of the element, not child elements.

foreach( $load->xpath("menu[link=". $link ." and tag=". $tag ." and text=". $text ."]") as $item ) { 

You may need to wrap your statement in quotes

foreach( $load->xpath(sprintf("menu[link=\"%s\" and tag=\"%s\" and text=\"%s\"]",$link,$tag,$text) as $item ) { 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for first comm! I did what you wrote, but it just goes back to the admin page (i had put header if the sscript finishes correctly), but the link/tag/text doesn't update if i change its value.. any ideas?
What you've said is beyond the scope of the original question, what I suggest is looking at your Object before and after your "foreach" loop to see if it's being updated, the other possibility is that you need quotes around your xpath vars (See my edit)
Ok i'll try it when i'm back home.. One question regarding your edit: what is the reason of using the back slashes before the quotes?
I've checked everything but nothing seems to work.. The double quote solution doesn't give anything different, and I don't seem to be able to check the result. The file gets updated (Notepad++ asks me to reopen the file) but the value doesn't change. Also I cant view the values of the array (It like: Array () ).. Is there anything I can do?
@Pr1nceJ4x if you start a string with double quotes and want to use double quotes within the string, you have to escape them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.