How to setup function in PHP with included file

时间:2016-02-12 20:06:32

标签: php

I've got 3 pages. I have made include("include/header.php) with the beggining of the HTML webpage + navigation. When the user was on the contact page than navigation link was underlined text-decoration: underline; My question is: How do I make the function, which will automaticly change the class - .underline when the user goes to that page? Thanks!

My header.php navigation code

<div class="navigation>
<a href="index.php" class="underline">Home<a> <!-- simulating that the user is on this page -->
<a href="about.php">About me</a>
<a href="contact.php">Contact me!</a>
</div>

My css code

.underline {
text-decoration: underline;
}

Code at index.php and other includingheader.php

<?php include("include/header.php") ?>

5 个答案:

答案 0 :(得分:2)

Try this:

<header>
  <nav>
    <a id="logo" href=".">
      <span>Company Name</span>
    </a>
    <a id="linkOne" href="#thingOne">
      <img src="…">
    </a>
    <a id="linkTwo" href="#thingTwo">
      <img src="…">
    </a>
    <a id="linkThree" href="#thingThree">
      <img src="…">
    </a>
  </nav>
</header>

答案 1 :(得分:1)

就个人而言,我对这种事情采取完全不同的方法。

首先,为您的链接设置一个页面数组,然后遍历它们,检查它是否是“当前”页面,并添加相关的类。

这种技术需要更少的代码来输出给定的链接,并且更容易添加更多的页面。

<?php
$pages = array(
    'Home'        => 'index.php',
    'About me'    => 'about.php',
    'Contact me!' => contact.php'
    // Now adding pages is easy, just add the relevant parts here
);
?>
<div class="navigation">
<?php 
    // Loop over each page from the array
    foreach($pages AS $title => $page) {
        // Set the class depending on which page is current
        $class = (strpos($_SERVER['REQUEST_URI'], $page) !== FALSE) ? 'underline' : '';
        // Output the link
        echo "<a href='{$page}' class='{$class}'>{$title}</a>";
} ?>
</div>

答案 2 :(得分:0)

In your include/header.php file, use this...

stream=True

答案 3 :(得分:0)

You have to get the requested url, you can use names(mydata)[which(colnames(mydata)=="Pr(>|t|)_oldvar")] <- "Pr_newvar" for that and your function should look like this

$_SERVER['REQUEST_URI']

and you call the function like this

function underlinePage($pageYouWant) {
  if ($_SERVER['REQUEST_URI'] == "/".$pageYouWant)) {
    return 'underline';
  }
}

答案 4 :(得分:0)

Yet another solution:

private static void SaveData(Dictionary<int, student> studentList)
{
    using (StreamWriter file = new StreamWriter(@"file address", true))
    {
        foreach (var item in studentList.)
        {
            file.WriteLine(item);
        }
    }
}