Jump to content

navigation with php


tomdelonge

Recommended Posts

i have my project set up in sort of "modules". i have one main file that "includes" multiple files.

for simplicity, here are the main ones:

 

header.php

navigation.php

footer.php

 

now in the navigation.php, i need to set a certain anchor to the class "current". i want to use php to do it, what's the best way? i want to leave navigation as one file. is there a simple way to insert php code into the class attribute and echo "current" or "", based upon something? what do other people do?

Link to comment
Share on other sites

I use the following code to do what you are trying to do. The pages that are included in the list are in the separate file "menuList.inc.php".

 

<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$fullURL = curPageURL();
if(!preg_match("|www|i","$fullURL")){
$parsedURL = parse_url($fullURL);
$addWWWURL = $parsedURL['scheme'] . "://www." . $parsedURL['host'] . "/";
}
require "menuList.inc.php";
$base=basename($_SERVER['PHP_SELF']);
if ($base != 'index.php'){
$menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$base."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$base."\" >$2</a></li>", $menu);
}else{
if(preg_match("|www|i","$fullURL")){
	$menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$fullURL."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$fullURL."\" >$2</a></li>", $menu);
}else{
	$menu=preg_replace("|<li><a title=\"(.*)\"\shref=\"".$addWWWURL."\">(.*)</a></li>|U", "<li id=\"active\"><a title=\"$1\" href=\"".$addWWWURL."\" >$2</a></li>", $menu);
}
}
$menu=preg_replace("|[\r\n\t]|", "", $menu);
echo $menu;
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.