2013-05-01 41 views
0

我目前有一个header.php页面,它存储我的页眉和布局的所有样式和位置。然后我有一个contactus.php页面,其中包含header.php页面。在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>标签内打印的类时,什么都不会发生。它执行include命令,并输出header.php中声明的值,而忽略我尝试更改新页面上的值。

是否有任何方法只改变contactus.php内的值,同时仍然保持header.php内的初始值,以便所有其他页面仍然可以使用'默认'风格?

编辑:内contactus.php,我可以更改从header.php获得(包括)变量的值,而不实际“改变”全局变量?

+0

哪里abouts你重写' $ classnamehere'变量? – billyonecan 2013-05-01 07:20:53

+0

我想从contactus.php中覆盖。 – 2013-05-01 07:24:08

+0

我只是使用file_get_contents()加载header.php,做一个搜索并替换“linkStyleOne”,然后eval()字符串。 – cleong 2013-05-01 08:27:39

回答

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包含在内,将$ classname设置为 '“linkStyleOne”',并创建链接类名为 linkStyleOne的实际链接。
  • 之后$ classnamehere设置为linkStyleTwo

这意味着,你必须指定类名之前包括header.php文件。

相反,它包括在contactus.php的,你可以的header.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' 
?> 
+0

谢谢你为我困惑。根据你所说的,我试图找出解决办法。你有什么想法 ? – 2013-05-01 07:40:48

+0

我设法找到解决办法。我从header.php中删除了$ classnamehere变量,而是在contactus.php中声明了该变量。每次我创建一个新页面时,它都需要多一点代码,但我想我可以......至少在我找到一种新方法之前。非常感谢。 – 2013-05-01 07:50:11

+0

@xa - 我用一个例子编辑了我的答案。这可能解决你的问题? – bestprogrammerintheworld 2013-05-01 07:51:25

0

改变这种

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

class= "<?php echo $classnamehere ; ?>" 
+1

或更短'class =“<?= $ classnamehere;?>”' – 2013-05-01 07:13:46

+0

我使用单引号和双引号,因为我所有其他的HTML样式标签都使用双引号。这样,这个变量名在输出时也会被双引号括起来。无论如何,这并不能解决我的问题。 – 2013-05-01 07:15:00

+0

@xa。你不能用单引号包围变量,单引号内的变量不会被插值。 – billyonecan 2013-05-01 07:22:23

0

或者使用这种方式

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

还是这样(林不知道这个工程)

<?php echo '"{$classnamehere}"' ; ?> 
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']值的子字符串对条件测试删除每个页面上的设置函数调用,并将条件设置为类的构造函数。

+0

嗯,一个例子会很好。我不明白你的回答非常好。 – 2013-05-01 07:29:03

+0

好吧,我要编辑我的答案,这将花一点时间...我将尽量简化一大套代码。 – Frank 2013-05-01 08:45:05

0

如果包含代码,包含代码中的所有变量都是全局变量,因此所有变量都会反映在所有部分中。 在标题。PHP

$a = 10; 

contactus.php

include "header.php" 
$a = 0; 

而且也没有改变$a只有内联系我们的方式。 $a更改后的所有代码都将使用新值。 你可以在里面建立联系我们临时变量$a_copy,然后改变这个变量:

$a_copy = $a; 
....class= <?php echo '"$a_copy"'... 
+0

好的,那么如何在输出旧值之前使用新值?因为那是发生的事情。在计算新值之前输出旧值。 – 2013-05-01 07:36:01

+0

我不认为,有一种方法。代码是“串行”,所以更改前的所有内容都使用旧值,之后是新值。对于这种行为,我建议您使用JavaScript并刷新站点的一部分。 – 2013-05-01 07:50:37