0

我目前有一个 header.php 页面,其中存储了我的标题和布局的所有样式和位置。然后我有一个包含 header.php 页面的contactus.php 页面。

-----CONTACTUS.PHP:
<?php 
    include 'header.php' 
    $classnamehere='"linkStyleTwo"'     //Here is where I want to update the value
?>

我页面顶部的主要链接以某种方式使用:

------HEADER.PHP:
<?php 
    $classnamehere= '"linkStyleOne"' ?>
    <a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......

我想更改该人单击的链接的样式,以便它弹出以指示该人他们在哪个页面上。目前,当我尝试更改 contactus.php 中 $classnamehere 的值(通过简单地为其分配一个新值)以更改<a href>标签内打印的类时,没有任何反应。它执行包含命令,并输出在 header.php 中声明的值,忽略我在新页面上更改值的尝试。

有没有办法只改变contactus.php中的值,同时仍然保持header.php中的初始值,以便所有其他页面仍然可以使用“默认”样式?

编辑:在contactus.php 中,我可以更改从header.php 获得(包含)的变量的值,而无需实际“更改”全局变量吗?

4

5 回答 5

1

查看您的代码:

   -----CONTACTUS.PHP:
    <?php 
        include 'header.php' 
        $classnamehere='"linkStyleTwo"'     //Here is where I want to update the value
    ?>

The main links at the top of my page are styled in a certain way using:

    ------HEADER.PHP:
    <?php 
        $classnamehere= '"linkStyleOne"' ?>
        <a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......

您的代码执行以下操作:

  • contactus.php 被执行。
  • header.php 包含在内,将 $classnamehere 设置为 '"linkStyleOne"' 并使用类名 linkStyleOne 创建实际链接。
  • 之后 $classnamehere 设置为 linkStyleTwo

这意味着您必须在包含 header.php 之前分配类名。

您可以在 header.php 中执行逻辑,而不是将其包含在 contactus.php 中:

<?php
if ($currentPage) == 'contact') { 
    //Set this class when user is on a specific page
    $classnamehere='"linkStyleTwo"';     
}
else {
    $classnamehere= '"linkStyleOne"';
}
?>

只需在contactus.php中执行此操作

<?php 
$currentPage = 'contact';
include 'header.php' 
?>
于 2013-05-01T07:36:54.180 回答
0

改变这个

 class= <?php echo '"$classnamehere"' ; ?>.

class= "<?php echo $classnamehere ; ?>"
于 2013-05-01T07:12:59.333 回答
0

或者这样使用

<?php echo '"', $classnamehere ,'"' ; ?>

或者这样(我不确定这是否有效)

<?php echo '"{$classnamehere}"' ; ?>
于 2013-05-01T07:17:09.203 回答
0

您应该创建一个 PHP 类并将其包含在您的标头中,或者将其设置为您的标头。在页面上调用它,然后为类变量分配值,然后您可以回显一个单独的类函数,该函数使用您需要的变量值。如果您不熟悉该过程,我可以提供一个示例。

Header.php 简单示例:

<?php

class Template
{
    private $active_nav;

    private __construct()
    {

    }

    public function set_active($page = '') // set page test value
    {
        $this->active_nav = $page;
    }

    public function get_active() // return page test value
    {
        return $this->active_nav;
    }

    public function nav_links() // this could be changed to return an entire header or part or whatever -- change the scope accordingly
    {
        // active link CSS is linSktyleOne
        $current_page = $this->get_active();
        $nav_links  = '<ul>';
        $nav_links .=   '<li><a href="index.php" class="' . ($current_page == 'index' ? "linkSytleOne" : "linkStyleTwo") . '">Index</a></li>';
        $nav_links .=   '<li><a href="contact.php" class="' . ($current_page == 'contact' ? "linkSytleOne" : "linkStyleTwo") . '">Contact</a></li>';
        $nav_links .= '</ul>';

        return $nav_links;
    }
}

contact.php 简单示例:

<?php

require_once('includes/header.php');

$template = new Template;
$template->set_active('contact');

// put all your html or other code here

echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well

index.php 简单示例:

<?php

require_once('includes/header.php');

$template = new Template;
$template->set_active('index');

// put all your html or other code here

echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well

所有这些帮助更有意义吗?您只需向每个页面添加 1 行代码,如果您真的想要花哨,您可以通过使用$_SERVER['REQUEST_URI']值的子字符串进行条件测试来删除每个页面上的 set 函数调用,并将条件设置在类的构造函数。

于 2013-05-01T07:22:40.973 回答
0

如果包含代码,则包含代码中的所有变量都是全局变量,因此所有更改都会反映在所有部分中。在header.php中

$a = 10;

contactus.php

include "header.php"
$a = 0;

并且没有办法$a只在contactus内部进行更改。更改后的所有代码$a都将使用新值。您可以在 contactus 中创建临时变量$a_copy,然后更改此变量:

$a_copy = $a;
....class= <?php echo '"$a_copy"'...
于 2013-05-01T07:22:55.710 回答