icant.co.ukに基づいたかなり単純なサイトのメニューをセットアップしました。5ページ程度で結構簡単です。この小さなサイトは、主にMATEを使用したいくつかのテーブル用の mysql ブラウザです。ヘッダーとフッターの HTML を含む common.php ファイルがあるので、そこに以下のコードを配置します。
以下のコードは、メニューの現在のページを強調表示します。それは醜いので、もっと良い方法が必要だと確信しています。
どんな助けでも大歓迎です、ありがとう!
ここに私のコードがあります
<?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>';
}
?>
更新好奇心旺盛な方のために、以下は作業コードです!
<?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;
?>