如何使用PHP从活动导航页面中删除锚点?

问题描述:

I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.

I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.

Here is my code:

  <div id="navigation_menu">
  <?
  foreach($pagedata->menu as $menuitem){
      $class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';

      ?>
      <div id="<?=$menuitem->uri?>" class="<?=$class?>">
        <img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
        <h1><a href="<?=PROTOCOL?>//<?=DOMAIN?>/<?=$menuitem->uri?>"><?=$menuitem->title?></a></h1>
        <h2><?=$menuitem->description?></h2>
        <img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
      </div>
      <?      
  }
  ?>
  </div>

Any help would be greatly appreciated. Thanks!

UPDATED CODE: (this is what works for me now)

<div id="navigation_menu">
    <?
  foreach($pagedata->menu as $menuitem){
      $class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
      ?>
      <div id="<?=$menuitem->uri?>" class="<?=$class?>">
        <img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
        <h1>
                <?php if ($menuitem->uri == $requesteduri):?>
            <?=$menuitem->title;?>
                <?php else: ?>
            <a href="<?=PROTOCOL?>//<?=DOMAIN?>/<?=$menuitem->uri?>"><?=$menuitem->title?></a>
                <?php endif;?>
                </h1>
        <h2><?=$menuitem->description?></h2>
        <img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
      </div>

      <?      
  }

  ?>
  </div>

I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)

<?php echo base_name($_SERVER['REQUEST_URI']); ?>

find out what your loop is outputting (Put this in the loop):

<?php echo $menuitem->uri; ?>

Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:

<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
   <a href="<?=PROTOCOL?>//<?=DOMAIN?>/<?=$menuitem->uri?>"><?=$menuitem->title?></a>
<?php else: ?>
   <?=$menuitem->title;?>
<?php endif;?>
</h1>

Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.