2011-06-01 184 views
0

我基本上试图在主页上隐藏DIV,但在其他地方显示(由URL控制)。根据URL(PHP)显示/隐藏Div

DIV名 “left_col”

试图做到这一点只使用PHP。 这将如何完成?

+0

可能重复(http://stackoverflow.com/questions/5216172/getting-current-url) – 2011-06-01 18:44:36

+0

你应该给你的问题更多的上下文。例如,您想要将DIV隐藏在主页上的哪个位置? – 2011-06-01 18:45:04

+0

谢谢尼古拉斯, 我不希望PHP直接编写代码...我只是希望它触发div的CSS属性,以“隐藏”或“可见” 因此...类似于: (strpos($ _ SERVER ['REQUEST_URI'],“index.php”)> = 0){div id ='left_col':hidden; div id ='banner':show; div id ='big_footer':show; } – Ice 2011-06-01 19:26:35

回答

4

假设你的首页网址index.php,这样的事情应该工作,很容易:

// We're NOT on the home page 
if (strpos($_SERVER['REQUEST_URI'], "index.php") >= 0) { 
    echo "<div id='left_col'>contents</div>"; 
} 

还有很多其他的方法可以做到这一点,取决于你的架构的其余部分,如果你使用的模板引擎或许多其他因素。如果你为你的问题和你的环境发布更多的上下文,我可以更具体。

编辑要做到这一点与CSS而不是完全禁止输出,此方法指定每个显示和隐藏的类,并将其应用于div。 [获取当前URL]的

// We're NOT on the home page 
if (strpos($_SERVER['REQUEST_URI'], "index.php") >= 0) { 
    $left_col_class = "showme"; 
} 
else { 
    $left_col_class = "hideme"; 
} 

// Your html... 
<div id='left_col' class='<?php echo $left_col_class; ?>'>contents</div> 

// Your CSS 
.hideme { display: none; } 
.showme { display: block; } 
+0

谢谢!这在某些情况下效果很好,但是我希望在div中触发CSS中的“hidden/show”属性。 – Ice 2011-06-01 18:59:52

+0

例如: if(strpos($ _ SERVER ['REQUEST_URI'],“index.php”)> = 0){div ID ='left_col':hidden; div id ='banner':show; div id ='big_footer':show; } – Ice 2011-06-01 19:07:16