PHP Dynamic Navigation

2015.11.03 Tutorials 1 Comment

Oops, so I wrote this bit of code months ago, but got lazy about writing the actual tutorial. Basically, if you don’t want to install a CMS and just want to have dynamic/variable page title or navigation highlight, where your current page is displayed differently in the navigation, so that your visitor knows which menu item they are on.

First, you want to grab the page name, be it page.php or index.php?page

$url = $_SERVER['REQUEST_URI'];

Then we need to parse it, depending on if it’s just page.php, or it has a query string (aka the stuff after the ? in page.php?query or page.php?q=query).

if (strlen($_SERVER['QUERY_STRING']) > 0)
$page = (strpos($url, "=") > 0) ? (substr($url, strpos($url, "=") + 1))
: (substr($url, strpos($url, "?") + 1));

In the if statement, we are testing if the length of the query is greater than 0, so if there is a query, presumably it will be greater than 0, after which we need to determine if it’s page.php?query or page.php?var=query (var being anything).

After the if statement, the syntax of the code works as follows, (true or false statement to test) ? (what to do if true) : (what to do if false); and the parentheses for better readability.

if you can find “=” in the string query:

(strpos($url, "=") > 0)

grab the stuff after “=”:

(substr($url, strpos($url, "=") + 1))

otherwise just grab the stuff after “?”:

>(substr($url, strpos($url, "?") + 1))

But if the if condition fails, or evaluates to false, then we fall to the else statement.

else {
preg_match('~\/(.*?)\.php~', $url, $output);
$page = $output[1];
while (strpos($page, "/") !== false)
$page = substr($page, strpos($page, "/") + 1);
}

First we use Regex to grab the stuff before “.php”, whatever it may be. The output is an array, so we assign the value of the page name into a non-array variable for better readability, and then loop through, using a while loop, to make sure there are no “/” in the page name. So if you can find “/” in the string, cut off what comes before it, turning “dir/page” into just “page.”

The complete PHP code is:

<?php
$url = $_SERVER['REQUEST_URI'];

if (strlen($_SERVER['QUERY_STRING']) > 0)
$page = (strpos($url, "=") > 0) ? (substr($url, strpos($url, "=") + 1))
: (substr($url, strpos($url, "?") + 1));

else {
preg_match('~\/(.*?)\.php~', $url, $output);
$page = $output[1];
while (strpos($page, "/") !== false)
$page = substr($page, strpos($page, "/") + 1);
}
?>

You can place this anywhere before the content of your page. Then in your HTML, if you’re doing navigation, add the following code:

<ul class="nav">
<li<?php if ($page == "index") echo ' class="active"'; ?>>
<a href="#">Home</a>
</li>
<li<?php if ($page == "network") echo ' class="active"'; ?>>
<a href="#">Network</a>
</li>
<li<?php if ($page == "links") echo ' class="active"'; ?>>
<a href="#">Links</a>
</li>
</ul>

Change the CSS class name accordingly, and “index,” “network,” and “links” to whatever you need. The embedded PHP is just matching the page title in the url, and then applying ‘ class=”active”‘ to the <li> tag.

If you would like to also display the page title in the <title> tag, add the following code, and change what you need accordingly:

<title>Site Title | <?php
if ($page == "index") echo "Index";
if ($page == "network") echo "Network";
if ($page == "links") echo "Links"; </title>
?>

Since nothing is “included” from the url query, you don’t need to worry about someone trying to include malicious code by type “page.php?URL_TO_NASTY_STUFF” since you are writing to the page exactly what you expect, and not a wildcard.

1 Comment

Kya

That’s a cool bit of code! :D Thanks for sharing this tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *