Month: November 2015

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).

Continue Reading