douzhanyan5015
douzhanyan5015
2010-07-24 12:44
浏览 110
已采纳

使用PHP隐藏和显示部分HTML?

The example I have in HTML:

<div id="red-nav-warp">
  <ul id="red-nav-logo">
    <li><img class="sponsors" id="sponsorone" src="media/img/logosmall.png" /></li>
  </ul>
  <ul class="clearfix" id="red-nav-list-member" >
    <li><?php $themeSys->Href('logout',$langSys->get('LOGOUT')); ?></li>
    <li><?php $themeSys->Href('settings',$langSys->get('SETTINGS')); ?></li>
  </ul>    
  <ul class="clearfix" id="red-nav-list" >
    <li><?php $themeSys->Href('home',$langSys->get('HOME')); ?></li>
    <li><?php $themeSys->Href('why',$langSys->get('WHY')); ?></li>
    <li><?php $themeSys->Href('register',$langSys->get('REGISTER')); ?></a></li>
    <li><?php $themeSys->Href('account',$langSys->get('MEMBER')); ?></a></li>
    <li><?php $themeSys->Href('community',$langSys->get('COMMUNITY')); ?></a></li>
    <li><?php $themeSys->Href('blog',$langSys->get('BLOG')); ?></a></li>
    <li><?php $themeSys->Href('partners',$langSys->get('PARTNERS')); ?></a></li>
  </ul> 

  <div class="clearfix"></div>    
</div><!-- END red-nav-warp -->

What I just want to show is a portion of HTML if he is a logged in user.

<div id="red-nav-warp">
  <ul id="red-nav-logo">
    <li><img class="sponsors" id="sponsorone" src="media/img/logosmall.png" /></li>
  </ul>

  <!-- hidden or no output -->  
  <ul class="clearfix" id="red-nav-list" >
    <li><?php $themeSys->Href('home',$langSys->get('HOME')); ?></li>
    <li><?php $themeSys->Href('why',$langSys->get('WHY')); ?></li>
    <li><?php $themeSys->Href('register',$langSys->get('REGISTER')); ?></a></li>
    <li><?php $themeSys->Href('account',$langSys->get('MEMBER')); ?></a></li>
    <li><?php $themeSys->Href('community',$langSys->get('COMMUNITY')); ?></a></li>
    <li><?php $themeSys->Href('blog',$langSys->get('BLOG')); ?></a></li>
    <li><?php $themeSys->Href('partners',$langSys->get('PARTNERS')); ?></a></li>
  </ul> 

  <div class="clearfix"></div>    
</div><!-- END red-nav-warp -->

Anyone have an idea or a example class that can hide things so that I can write it like ( better an class if it's possible.)

<div id="red-nav-warp">
  <ul id="red-nav-logo">
    <li><img class="sponsors" id="sponsorone" src="media/img/logosmall.png" /></li>
   </ul>
   <?php $hideSys->hiddenbelow(); ?> <!-- some hide class or some magic -->
   <ul class="clearfix" id="red-nav-list-member" >
     <li><?php $themeSys->Href('logout',$langSys->get('LOGOUT')); ?></li>
     <li><?php $themeSys->Href('settings',$langSys->get('SETTINGS')); ?></li>
   </ul>
   <?php $hideSys->stop(); ?> <!-- end some hide class or some magic -->   
   <ul class="clearfix" id="red-nav-list" >
     <li><?php $themeSys->Href('home',$langSys->get('HOME')); ?></li>
     <li><?php $themeSys->Href('why',$langSys->get('WHY')); ?></li>
     <li><?php $themeSys->Href('register',$langSys->get('REGISTER')); ?></a></li>
     <li><?php $themeSys->Href('account',$langSys->get('MEMBER')); ?></a></li>
     <li><?php $themeSys->Href('community',$langSys->get('COMMUNITY')); ?></a></li>
     <li><?php $themeSys->Href('blog',$langSys->get('BLOG')); ?></a></li>
     <li><?php $themeSys->Href('partners',$langSys->get('PARTNERS')); ?></a></li>
   </ul> 

   <div class="clearfix"></div>    
</div><!-- END red-nav-warp -->

What I have done, and yes I know it's an epic fail at least I'm trying :)

<?php  
/**
* TRYING TO HIDE SOME PART OF HTML
* useing if $_Session['login'] = true;
*/
class Hideing
{
  function __construct()
  {

  }

  function hiddenbelow()
  {
    return "if($_Session['login']){"; // epic fail
  }

  function stop()
  {
    return "}";
  }
}

$hideSys = new Hideing;
?>

Thanks for looking in,

Adam Ramadhan

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujia4619
    doujia4619 2010-07-24 12:49
    已采纳

    Your code just returns a string. PHP will not execute it automatically.

    You need to do

     </ul>
     <?php if($_Session['login']) { ?> <!-- some hide class or some magic -->
     <ul class="clearfix" id="red-nav-list-member" >
      <li><?php $themeSys->Href('logout',$langSys->get('LOGOUT')); ?></li>
      <li><?php $themeSys->Href('settings',$langSys->get('SETTINGS')); ?></li>
     </ul>
     <?php } ?> <!-- end some hide class or some magic -->   
     <ul class="clearfix" id="red-nav-list" >
    

    You may have read about the wisdom of seperating Model View and Controller logic and want to encapsulate that logic into a class, but you still need ifs in the PHP code.

     </ul>
     <?php if($hideSys->isLoggedIn()) { ?> <!-- some hide class or some magic -->
     <ul class="clearfix" id="red-nav-list-member" >
      <li><?php $themeSys->Href('logout',$langSys->get('LOGOUT')); ?></li>
      <li><?php $themeSys->Href('settings',$langSys->get('SETTINGS')); ?></li>
     </ul>
     <?php } ?> <!-- end some hide class or some magic -->   
     <ul class="clearfix" id="red-nav-list" >
    

    Where the isLoggedIn() method returns a Boolean true or false variable.

    点赞 评论
  • douyan8961
    douyan8961 2010-07-24 12:48

    wrap the code you want to hide with:

    <?php if ($_SESSION['login']): ?>
    <!-- code goes here -->
    <?php endif; ?>
    
    点赞 评论

相关推荐