2013-06-01 256 views
0

已經搜索了與此問題有關的所有主題,但沒有任何答案幫助我,所以我發佈了一個新問題。PHP在特定頁面上隱藏div

我有我的網頁index.php有php包括所有的導航鏈接打開在同一個index.php。例如,當我點擊關於我們時,它會打開index.php?page = aboutus。

我的問題是我有我的主頁一個div:

<div class="icons_small"> 
<a href="#"><img src="images/layout/facebook_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/twitter_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/youtube_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/location_icon_s.png" width="75" height="67" /></a> 
</div> 

對於這個div,我希望他隱藏在index.php文件,但我想這是在所有其他網頁像指數可見。 php?page = aboutus,index.php?page = contact等

有沒有簡單的方法來做到這一點與PHP? :)

Thx提前。

回答

1

試試這個。如果GET參數page未設置或沒有值,則會隱藏div。

<?php 
if(isset($_GET['page']) && $_GET['page'] != "") { 
?> 
<div class="icons_small"> 
<a href="#"><img src="images/layout/facebook_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/twitter_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/youtube_icon_s.png" width="75" height="67" /></a> 
<a href="#"><img src="images/layout/location_icon_s.png" width="75" height="67" /></a> 
</div> 
<?php 
} 
?> 
+0

這就像一個魅力。 Thx很多:) – FallenRider

+0

沒問題。如果我的答案已解決您的問題,請選擇它作爲接受的答案。 –

+0

@FallenRider如果這解決了你的問題,你應該標記這[答案已被接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – karthikr

0

這取決於頁面的包含方式。

如果我們使用PHP的echo來打印出HTML,我們可以簡單地選擇不打印頁面沒有設置(即沒有0​​)時的echo

if(!isset($_GET['page']) 
{ 
    echo 'The contents of the div that shouldn't be on the page'; 
} 

如果因任何原因,你不能有選擇地打印部分,一個凌亂替代方案是呼應一些JavaScript這將去除股利。但是,這有一個關鍵的缺陷,即div將仍然顯示給禁用JavaScript的用戶。

0

這根本不安全。我明白這一切都是關於4張圖片的,但從不良習慣中學習對其他項目可能是致命的。

如果我寫"index.php?page=I_WANT_TO_SEE_THAT_BOX"會發生什麼?

我會建議的是,檢查你是否應該直接在你的包含函數中顯示它。我假設你有類似

function IncludeView() { 
    $allowed_pages = array("view","friends","foo","bar"); 
    if(isset($_GET['page'])) { 
    if(in_array($_GET['page'],$allowed_pages)) { 
     include_once(PAGE_DIR."/".$_GET['page'].".php"); 
     define("IS_PAGE_OKAY",true); 
    } 
    } 
    else header(BASE_URL); 
} 
IncludeView(); 

在你的模板之後:

<?php if(defined("IS_PAGE_OKAY")) { ?> 
    //your html code that is shown only on other pages than index.php 
<?php } ?>