2013-05-01 38 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