views:

439

answers:

4

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;

?>

+2  A: 

What I normally do is something like (for all elements...):

<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
jeroen
looks simple enoough, tring that now. thank you!
shaiss
+1, certainly more wieldy that echoing huge chunks of (mostly) redundant markup.
karim79
I agree karim79, I'm no pro. Any suggestions on getting the current page in 1 line?
shaiss
@shaiss - I wasn't dissing your solution - I was complimenting the answer :) don't get me wrong.
karim79
I understand. I commend the answer too!
shaiss
@shaiss : I think the way you are getting the current page is fine and as your links are hard-coded, I don´t see what you could gain there. Unless you put your links and link-texts in an array or an object and you loop through them, comparing each link in the loop to $currentFile.
jeroen
+2  A: 

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 = '<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;
emkee
that's what I was looking for, testing it out now. For some reason I'm getting a syntax error on for ($pages as $page) { `Parse error: syntax error, unexpected T_AS, expecting ';'`
shaiss
Try the code now, it contained few minor errors :)
emkee
Cool, that worked. I had to add in the id="current" part. Thank you for the help!
shaiss
+1  A: 

A more concise way of doing it (if you have short tags enabled) would be:

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

It performs the same function as the first answer, just more concisely.

BraedenP
BraedenP, this is interesting, but I'm a bit lost. My server does have short tag support, however I'm not understanding the `$test` or `'selected' : 'not_selected'` parts. Thanks for the help!
shaiss
Basically, it checks to see if the variable $test equals the "your_page_name" value. If it does, it echo's the text (or a function if you choose) that's after the question mark. If $test does not equal "your_page_name" then the text is echo'ed or the function is run that's located after the colon. It's known as the "ternary operator" http://ca3.php.net/ternary#language.operators.comparison.ternary
BraedenP
Thank you for the clarification, I'll look into that some more.
shaiss
A: 

Here's a snippet from a project of mine. It it old ugly code, and uses tables, but you can just as easily use the idea for divs and cleaner markup. The trick is to make the navigation use a different class if the current page matches it's 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>
Eli