1

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;

?>

4

4 に答える 4

3

私が通常行うことは(すべての要素に対して...)のようなものです:

<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
于 2009-10-09T21:26:05.213 に答える
2

それがあなたの意図したものかどうかはわかりませんが、このようにして、この醜い if-else を取り除くことができます:

$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" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
   $activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
   $menuOutput .= '<li' . $activeAppend . '>'
               .  '<a href="' . $page['file'] . '">' . $page['title'] .'</a>'
               .  '</li>'; 
}           
$menuOutput .= '</ul>';

echo $menuOutput;
于 2009-10-09T21:37:03.143 に答える
2

より簡潔な方法 (短いタグを有効にしている場合) は次のようになります。

<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>

最初の回答と同じ機能を、より簡潔に実行します。

于 2009-10-09T21:44:52.937 に答える
-1

これが私のプロジェクトのスニペットです。古い醜いコードであり、テーブルを使用していますが、div とクリーンなマークアップのアイデアを同じように簡単に使用できます。トリックは、現在のページがその URL と一致する場合、ナビゲーションで別のクラスを使用するようにすることです。

    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
    <td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>
于 2009-10-09T22:13:31.023 に答える