Forum Home
PC World Chat
 
Thread ID: 121476 2011-10-27 08:47:00 PHP help needed bot (15449) PC World Chat
Post ID Timestamp Content User
1240278 2011-10-27 08:47:00 Hi,

I am new to PHP. I am running a website which I want to convert to PHP as my current site is a PITA. Currently, the menu is copied and pasted onto each HTML document. The only difference is that the currently selected menu items div ID is "selectedmenuitem" as opposed to "menuitem". What I want is a PHP file with a function, that, when given the page title, finds the menu item from the menu HTML and replaces "menuitem" with "currentmenuitem".

If the menu HTML is this:

<div id="mainmenu">
<div id="menuitem"><a>Home</a></div>
<div id="menuitem"><a>About</a></div>
<div id="menuitem"><a>Blog</a></div>
</div>

Then, if the function is called with "Home", I want it to return:

<div id="mainmenu">
<div id="currentmenuitem"><a>Home</a></div>
<div id="menuitem"><a>About</a></div>
<div id="menuitem"><a>Blog</a></div>
</div>

Or, if it's called with "About", I want it to return:

<div id="mainmenu">
<div id="menuitem"><a>Home</a></div>
<div id="currentmenuitem"><a>About</a></div>
<div id="menuitem"><a>Blog</a></div>
</div>

Any ideas?
bot (15449)
1240279 2011-10-27 10:18:00 OK, firstly you should know the following: You're asking the wrong question, this isn't how you should be approaching the problem - you may want to do a bit of research first.
Your HTML is invalid, you should never have more than one instance of the same ID in a given document (you should be using classes).
While syntactically valid, you shouldn't be using divs to mark up a list (you should be using list tags for that).

That said, here's a function that will do exactly what you asked for:
function bot_menu($menuHTML, $currentPage) {
$d = new DOMDocument();
$d->loadXML($menuHTML);
foreach($d->documentElement->childNodes as $child) {
if(!($child instanceof DOMElement) || $child->tagName != 'div')
continue;
if($child->nodeValue == $currentPage) {
$child->setAttribute('id', 'currentmenuitem');
return $d->saveXML();
}
}
}Given the menu HTML example above, and the page name in the manner you describe, this function will return the modified menu HTML.

Note that I strongly advise you not to use this - you'd be far better off figuring out why your current approach is a bad idea and learning how to do it properly.
Erayd (23)
1240280 2011-10-27 10:44:00 Sorry, I didn't have the right HTML;



<li class="menuitem"><a class="menulink" href="index.html">Home</a></li>


Therefore, if the text inside the <a> matches $currentPage, the class attribute should be currentmenuitem.
bot (15449)
1240281 2011-10-27 11:11:00 Assuming you meant that your menu HTML is now:
<ul id="mainmenu">
<li class="menuitem"><a class="menulink" href="index.html">Home</a></li>
<li class="menuitem"><a class="menulink" href="index.html">About</a></li>
<li class="menuitem"><a class="menulink" href="index.html">Blog</a></li>
</ul>
Then the PHP function you asked for will now be:
function bot_menu($menuHTML, $currentPage) {
$d = new DOMDocument();
$d->loadXML($menuHTML);
foreach($d->documentElement->childNodes as $child) {
if(!($child instanceof DOMElement) || $child->tagName != 'li')
continue;
if($child->nodeValue == $currentPage) {
$child->setAttribute('class', 'currentmenuitem');
return $d->saveXML();
}
}
}

Note that this is still utterly the wrong approach to be taking - while your HTML is now correct, this really isn't how you should be doing it - you need to do some research on how to properly create dynamic pages.
Erayd (23)
1240282 2011-10-28 03:43:00 Hi,

I am new to PHP. I am running a website which I want to convert to PHP ?
Cheat and use one of those things like sitebuilder to make it?
pctek (84)
1240283 2011-10-28 05:25:00 NO. bot (15449)
1