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
https://forums.phpfreaks.com/topic/120134-navigation-with-php/
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
https://forums.phpfreaks.com/topic/120134-navigation-with-php/#findComment-618954
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.