Jump to content

Include Script Whilst Activating Style, Help!?


Jezthomp

Recommended Posts

Hello i wonder if anyone could help me with this little but essentially very useful problem?

 

At the moment i currently use a simple include php script.

 

<?php require_once('includes/Nav.php'); ?>

 

For the usual reason of when the site gets big i only need to change 1 file.

 

However, i now need to include a navigation which also has a a:active style on it, i.e when on that page it tells you with the style.

 

Is there anyway i can still include this navigation, i cannot think of how to do it because the file will be the same on every page so how will it know its active?

 

I asked on another forum where someone was kind enough to help me and we came up with the following...but it doesn't work.

 

With one home page. testHome.php one include testInc.php and another page test1.php to link to.

 

Both the testHome and test1 have the testInc however, when you click between them nothing happens.

 

I popped it online here

 

and the code is as follows....

 

testInc.php

 

<?php
  function classChange($page,$class){
   if(stristr($_SERVER['REQUEST_URI'],$page)){
       return 'class="'.$class.'"';
  }
}
?>
  <div id="pageNumbers">
    <p><a href="/testHome.php" <? echo classChange('/testHome','selected'); ?>>test1</a>
     <a href="/test1.php" <? echo classChange('/test1.php','selected'); ?>>test2</a>
     </p>
  </div>

 

testHome and test1..

 

<title>Untitled Document</title>
<link href="styles/master.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php require_once('includes/testInc.php'); ?>


Test 1
</body>
</html>

 

and just incase the css...

 


div#pageNumbers a{
color: Black;
text-decoration: none;
}
div#pageNumbers a:hover, #pageNumbers a:focus, #pageNumbers a:active, #pageNumbers a.selected{
color: Purple;
}

 

It just doesn't work, the link doesn't remain purple when on the appropriate page! :(

 

Any ideas?

 

Link to comment
Share on other sites

I would suggest the simplest method is to define a variable which is the name of page that is doing the including, which you set just before you include your navigation page. Something along the lines of:

 

<?php
$current_page = 'somepage.php';
include('includes/Nav.php');
//the rest of your code
?>

 

Then, Nav.php:

 

<?php
$pages = array('link name for page 1'=>'page1.php','Name of The Link for Page'=>'somepage.php','Foo bar'=>'foobar.php');//using an array will make it simpler to add new pages
print_r($pages);
foreach($pages as $k => $v){
	echo '<a href="'.$v.'" class="';
	echo ($v=$current_page)? 'normal' : 'purple';
	echo '">'.$k.'</a>';
}
?>

 

You'd then define two classes for your links. I've called them normal and purple here. Notice how ive set the array up so the key is the title for the page(e.g. the text of the link), whilst the value is the actual page.

Link to comment
Share on other sites

Thanks for that, i'm sorry if i am being dumb here but that seems to be more complicated than the one that should work but doesn't.

 

Does it work for yourself, i understand that more than yours. I just dont understand why its not working :(

 

I wanted to still control the link and the active style with the css style sheet and just have an include for the nav itself.

 

Thanks again :)

Link to comment
Share on other sites

Ive only taken a quick look at your code, but i can certainly see an issue with the css. If the page is the active one, you give the link the class name 'selected'. Yet you don't appear to define this class in your css code.

 

I have defined it here....

 

div#pageNumbers a:hover, #pageNumbers a:focus, #pageNumbers a:active,#pageNumbers a.selected{
color: Purple;
}

 

With help i have managed to crack the problem with the below working fine now :)

 

<?php
  function classChange($page,$class){
   if(stristr($_SERVER['PHP_SELF'],$page)){
       return 'class="'.$class.'"';
  }
}
?>
  <div id="pageNumbers">
    <p><a href="/Recent/Vietnamsp.php" <? echo classChange('Recent/Vietnamsp.php','selected'); ?>>1</a>
</div>

 

Thanks for all your help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.