簡體   English   中英

PHP-如果語句,相同的變量

[英]php - if statement, same variable

我目前正在檢查用戶的當前頁面,並將.css類應用於正確的菜單項。

我的問題是,我怎樣才能做到最好? 目前,我有這個:

    $currentpage = $_SERVER['PHP_SELF'];
  if($currentpage == "/prefs.php") $active = "active";
  if($currentpage == "/acc.php") $active = "active";
  if($currentpage == "/forum.php") $active = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active} tipTip'>

但這只會將active類添加到所有錨元素。

簡單的方法是在HTML內聯添加if語句:

<?php $currentpage = $_SERVER['PHP_SELF']; ?>

<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/prefs.php") echo "active "; ?>tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/acc.php") echo "active "; ?>tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='<?php if($currentpage == "/forum.php") echo "active "; ?>tipTip'>

盡管這可能比其他選項可讀性差。

為變量指定不同的名稱:

$currentpage = $_SERVER['PHP_SELF'];

if($currentpage == "/prefs.php") $active1 = "active";
if($currentpage == "/acc.php") $active2 = "active";
if($currentpage == "/forum.php") $active3 = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active1} tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='{$active2} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active3} tipTip'>
<?php
$currentpage = $_SERVER['PHP_SELF'];

echo "<a href='prefs.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/prefs.php' ? 'active' : '') . " tipTip'>
      <a href='acc.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/acc.php' ? 'active' : '') . " tipTip'>
      ...";

如果您不喜歡這樣,還可以使用switch() { case '': break; } switch() { case '': break; }設置。

獲取自我功能:

function getSelf($dir=""){
    if($dir==""){ $dir = $_SERVER['PHP_SELF']; }
    $self = explode("/",$dir);
    return $self[count($self)-1];
}

然后是菜單的分隔符:

<?php
                    $cls="";
                    $quick = "index.php||other_page.php||other_page2.php";
                    $stack = explode("||",$quick);
                    if(in_array(getSelf(), $stack)){ $cls = " active"; } else { $cls = ""; }
                ?>

然后class="<?php echo $cls; ?>"

有一種略有不同的方法,但是它也需要JavaScript(我知道您沒有在標簽中列出JavaScript,但仍然可以發現它很有用)。 技巧是使頁面名稱響應(修改的)DOM元素ID或類。

// or just make a simple function that strips the name from the $currentpage
// if the names are consistent like in your example
if ($currentpage == "/prefs.php") $id="prefs"; 
elseif ($currentpage == "/acc.php") $id="acc";
elseif ($currentpage == "/forum.php") $id="forum"; 

然后,進行JavaScript調用以找到適當的錨元素,並為其分配“活動”類。 這是一個jQuery示例:

var active = '<?php echo $id; ?>';
$('a .' + active).addClass('active'); // for class name
$('#' + active).addClass('active'); // for id, you get the point

這只是一個簡單的示例,但是我認為這可能會有所幫助,並且如果您使用JavaScript,則可使代碼更具可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM