PDA

View Full Version : Resolved Onclick "selected" navigation



CChawps
07-25-2011, 08:39 PM
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:


<?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 :)

jscheuer1
07-26-2011, 05:22 AM
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:


<?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>

CChawps
07-28-2011, 12:03 AM
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

jscheuer1
07-28-2011, 12:48 AM
If it's the default, make it so. Where we have:


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

do like:


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

CChawps
07-28-2011, 01:24 AM
Perfect! Thanks :)

traq
07-28-2011, 02:01 AM
some suggestions to streamline this:


<?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>