How to properly build a navigation menu that highlights the current page

前端 未结 4 712
栀梦
栀梦 2021-02-06 19:40

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.

4条回答
  •  [愿得一人]
    2021-02-06 20:16

    Not sure if that's what you meant, but this way you'll get rid of this ugly 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 = '
      '; foreach ($pages as $page) { $activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : ""; $menuOutput .= '' . '' . $page['title'] .'' . ''; } $menuOutput .= '
    '; echo $menuOutput;

提交回复
热议问题