如何使用php include文件向元素添加活动类

时间:2014-03-26 02:20:35

标签: php html css include

我有侧栏的sidebar-right.html文件,其中包含以下元素。

<div class="col-sm-2">
  <div class="list-group">
     <a href="Sub-Investment.php" class="list-group-item">New investment plan</a>
     <a href="open-creditcard.php?action=credit" class="list-group-item">Apply credit card</a>
     <a href="open-creditcard.php?action=limit" class="list-group-item">Increase credit limit</a>
  </div>
</div>
</div>
</div>
</div>

我在另一个名为open-creditcard.php的文件中调用此页面 使用include "..\View\\sidebar-right.html";

因此,此页面的最终网址如下:
http://localhost/obis/Controller/open-creditcard.php?action=limithttp://localhost/obis/Controller/open-creditcard.php?action=credit

我想使用php动态地在active的列表组中添加一个类sidebar-right.html

我尝试过这样的事情:
<a href="open-creditcard.php?action=credit" class="list-group-item" <?php if($_SERVER['PHP_SELF'] == "/open-creditcard.php?action=credit") echo 'class="list-group-item active"'; ?>>Apply credit card</a>
但它不起作用。有什么我想念的吗?有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

$_SERVER['PHP_SELF']不包含查询字符串,但您可以改为使用$_GET['action']

<a href="open-creditcard.php?action=credit" class="list-group-item<?= isset($_GET['action']) && $_GET['action'] == 'credit' ? ' active' : '' ?>">Apply credit card</a>
<a href="open-creditcard.php?action=limit" class="list-group-item<?= isset($_GET['action']) && $_GET['action'] == 'limit' ? ' active' : '' ?>">Increase credit limit</a>

答案 1 :(得分:-1)

<a href="open-creditcard.php?action=credit" class="list-group-item<?= isset($_GET['action']) && $_GET['action'] == 'credit' ? ' active' : '' ?>">Apply credit card</a>
<a href="open-creditcard.php?action=limit" class="list-group-item<?= isset($_GET['action']) && $_GET['action'] == 'limit' ? ' active' : '' ?>">Increase credit limit</a>