Results 1 to 6 of 6

Thread: Onclick "selected" navigation

  1. #1
    Join Date
    Jul 2011
    Location
    Denmark
    Posts
    18
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Onclick "selected" navigation

    Okay, I need a simple solution on how to change fx the class of a navigation
    tab, to show that it's selected. I use PHP for changing the content of my
    website (under development), so I don't have too copy the design of the page
    a lot of times. So when you click one of the navigation buttons the content, in
    the content area changes:

    HTML Code:
    <?php 
    if (empty($_GET["site"])) { 
    	$_GET['site']="home"; 
    } 
    if (!is_file("content/".$_GET['site'].".php") || eregi("../",$_GET['site'])) { 
    	$_GET['site']="404"; header("HTTP/1.0 404 Not Found"); 
    }
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>CChawps</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
    
    <nav>
        <a href="index.php?site=site1">Site 1</a>
        <a href="index.php?site=site2">Site 2</a>
        <a href="index.php?site=site3">Site 3</a>
        <a href="index.php?site=site4">Site 4</a>
    </nav>
    
    <div id="content">
    <?php
    include("content/".$_GET['site'].".php");
    ?>
    </div>
    
    </body>
    </html>
    Thanks
    Last edited by CChawps; 07-28-2011 at 01:24 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This looks like more of a PHP question to me. If the page is reloading, why not just use the GET to set a class on the current link, like:

    Code:
    <?php 
    $site = isset($_GET["site"])? $_GET["site"] : '';
    if (empty($_GET["site"])) { 
    	$_GET['site']="home"; 
    } 
    if (!is_file("content/".$_GET['site'].".php") || eregi("../",$_GET['site'])) { 
    	$_GET['site']="404"; header("HTTP/1.0 404 Not Found"); 
    }
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>CChawps</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
    
    <nav>
        <a<?php if($site === 'site1') echo ' class="selected"'; ?> href="index.php?site=site1">Site 1</a>
        <a<?php if($site === 'site2') echo ' class="selected"'; ?> href="index.php?site=site2">Site 2</a>
        <a<?php if($site === 'site3') echo ' class="selected"'; ?> href="index.php?site=site3">Site 3</a>
        <a<?php if($site === 'site4') echo ' class="selected"'; ?> href="index.php?site=site4">Site 4</a>
    </nav>
    
    <div id="content">
    <?php
    include("content/".$_GET['site'].".php");
    ?>
    </div>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2011
    Location
    Denmark
    Posts
    18
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    Hey John, it works. There is one problem tho. When I first enter the website
    the PHP automatically get the site called "site1", but the domain still remain
    unchanged. So the class is not applied when you enter the site at first.
    However if the first link is clicked one more time, the class is applied.

    I know zero of PHP besides using include, please help

    Watch here:

    www.cchawps.comuf.com

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If it's the default, make it so. Where we have:

    Code:
    <?php 
    $site = isset($_GET["site"])? $_GET["site"] : '';
    if (empty($_GET["site"])) { 
    	$_GET['site']="home"; 
    } 
    if (!is_file("content/".$_GET . . .
    do like:

    Code:
    <?php 
    $site = isset($_GET["site"])? $_GET["site"] : 'site1';
    if (empty($_GET["site"])) { 
    	$_GET['site']="home"; 
    } 
    if (!is_file("content/".$_GET . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    CChawps (07-28-2011)

  6. #5
    Join Date
    Jul 2011
    Location
    Denmark
    Posts
    18
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    Perfect! Thanks

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    some suggestions to streamline this:
    PHP Code:
    <?php 
    // pages user is allowed to navigate to
    // assoc. array with "filename" => "File Title" pairs
    // also used to make navigation, so adding/removing pages is easy
    $nav = array('site1'=>'Site 1','site2'=>'Site 2','site3'=>'Site 3','site4'=>'Site 4');

    // check if request is set and is allowed
    // if not, leave blank (your 404 will catch it)
    // or you could define a default page as you were discussing above
    $site = isset($_GET['site']) && in_array($_GET['site'], $nav)? $_GET['site']: '';

    // the above eliminates the need for your eregi() function
    // which, BTW, is _depreciated_: you should use preg_match() instead.

    // loop through menu items
    foreach($nav as $key => $val){
        
    // if current menu item matches current page, add "selected" class
        // otherwise leave it blank
        
    $selected $key === $site' class="selected"''';
        
    // build hyperlink
        
    $navlinks[] = '<a href="index.php?site='$key .'"'$selected .'>'$val .'</a>';
    }

    $links implode("\n",$navlinks);

    if(!
    file_exists('content/'.$site.'.php')){ $site '404'header("HTTP/1.0 404 Not Found"); }

    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <!-- you need to declare your charset -->
        <title>CChawps</title>
        <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <nav>
            <?php print $links?>
        </nav>
        <div id="content">
            <!-- incidentially, I would use absolute URL here. 
                 like "http://mysite.com/content/".$site.".php" -->
            <?php include("content/"$site .".php"); ?>
        </div>
    </body>
    </html>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •