Forum Moderators: coopster

Message Too Old, No Replies

Active Links for Included Content

         

Susie_Beth

9:20 pm on May 15, 2010 (gmt 0)

10+ Year Member



Hello there,

I am fairly new to PHP, and I have recently begun using the include function to add different content to my pages. Is there a way to create an active state for my links based on the content that is being included?

Here is the code I am experimenting with:

<div>
<ul>
<li> <a href="index.php?page=test01"/>Test 01</a></li>
<li> <a href="index.php?page=test02"/>Test 02</a></li>
<li> <a href="index.php?page=test03"/>Test 03</a></li>
</ul>
</div>
<div>
<?php
if(isset($_GET['page']) && file_exists($_GET['page'].".php")) {
include ($_GET['page'].".php");
}
elseif(empty($_GET['page'])) {
echo "No Pages Selected";
}
?>
</div>


For example, I would like the link "Test 01" to be highlighted a different color when test01.php is included. Normally I create active states by adding a class to the corresponding link on each separate page, but now that I am including everything into one page, I am not sure how to accomplish this.

Any help would be greatly appreciated!

Susie_Beth

Readie

10:20 pm on May 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The way I do this is I have an array of all my links, and a variable "key" on all of my pages, that correlates to the array. For example:

HOME PAGE:
-----
<?php

$ident = 'home';

echo 'Hello there!';

?>
NAVIGATION CONTROL DOCUMENT:
-----
<?php

$links = array(
array('Home Page', '/', 'home'),
array('News', '/news', 'news'),
array('Site info', '/info', 'info')
);

$count = count($links);
for($i = 0; $i < $count; $i++) {
$highlight = (isset($ident) && $ident === $links[$i][2])? 'active' : 'link';
echo '<br><a class="' . $highlight . '" href="' . $links[$i][1] . '">' . $links[$i][0] . '</a>';
}

?>
And the above would set the <a class="active" for the current page, and <a class="link" for all the others.

Susie_Beth

6:45 pm on May 16, 2010 (gmt 0)

10+ Year Member



Thanks! It works great!

Susie_Beth

Susie_Beth

7:58 pm on May 16, 2010 (gmt 0)

10+ Year Member



Now that I am implementing this code into my site, I am having a slight problem. The code works fine as long as the document is included before the navigation. Like this:

<div>
<?php
if(isset($_GET['page']) && file_exists($_GET['page'].".php")) {
include ($_GET['page'].".php");
}
elseif(empty($_GET['page'])) {
$ident = 'home';
echo "No Pages Selected";
}
?>
</div>

<div>
<?php

$links = array(
array('Home Page', 'index.php','home'),
array('Test 01', 'index.php?page=test01', 'test01'),
array('Test 02', 'index.php?page=test02', 'test02')
);

$count = count($links);
for($i = 0; $i < $count; $i++) {
$highlight = (isset($ident) && $ident === $links[$i][2])? 'active' : 'link';
echo '<br><a class="' . $highlight . '" href="' . $links[$i][1] . '">' . $links[$i][0] . '</a>';
}

?>
</div>


However, I would like the div with my navigation to positioned before the div with the include function. Like this:


<div>
<?php

$links = array(
array('Home Page', 'index.php','home'),
array('Test 01', 'index.php?page=test01', 'test01'),
array('Test 02', 'index.php?page=test02', 'test02')
);

$count = count($links);
for($i = 0; $i < $count; $i++) {
$highlight = (isset($ident) && $ident === $links[$i][2])? 'active' : 'link';
echo '<br><a class="' . $highlight . '" href="' . $links[$i][1] . '">' . $links[$i][0] . '</a>';
}

?>
</div>

<div>
<?php

if(isset($_GET['page']) && file_exists($_GET['page'].".php")) {
include ($_GET['page'].".php");
}
elseif(empty($_GET['page'])) {
$ident = 'home';
echo "No Pages Selected";
}
?>
</div>

Unfortunately, this does not work since the document must be included before $ident can be identified and the appropriate classes assigned. Is there a way that that the links can be positioned before the include function?

Thank you for your help!

Susie_Beth

Readie

8:17 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only method I can think of off the top of my head is to have the included content as a variable, rather than a echo, so home.php may look like:

<?php

$ident = 'home';
$content = 'Hello! Welcome to my site!';

?>
And then, where the content would go, you have

echo (isset($content) && !empty($content))? $content : 'Error';

Matthew1980

9:10 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie & susie_beth,

Don't forget to use strip_tags($_GET) on the data coming in, good habit to get into sanitising user generated input ;)

Also, wouldn't a an if(), elseif(), elseif(), else be advisable here, especially if you want to have a default page if someone requested a page that wasn't there, you need to code for exceptions... Just my opinion there...

Cheers,
MRb