I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
Any help is appreciated, thank you!
heres my code
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active"><a href="orders.php" id="current">Orders</a></li>';
}
else{
echo '<li><a href="orders.php">Orders</a></li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active"><a href="customers.php" id="current">Customer List</a></li>';
}
else{
echo '<li><a href="customers.php">Customer List</a></li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active"><a href="order_details.php" id="current">Order Details</a></li>';
}
else{
echo '<li><a href="order_details.php">Order Details</a></li>';
}
?>
UPDATE For those curious, below is the working code!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '<a href="' . $page['file'] . '"' . $currentAppend . '">' . $page['title'] .'</a>'
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>