Hide elements for certain groups

Permalink 1 user found helpful
Hey guys,
I've been working with HTML/CSS for a while, but all this PHP is new to me. For a part of my website, I want content enclosed in a <div> to be hidden or displayed depending on whether a user is part of a certain group. How on earth do I do that? There are only two groups (students, teachers) and I want certain div elements only viewable for teachers. Please help. Thanks in advance!

For example, would this hide the logo if the user is part of the group titled "students":
<?php
       $u = new User();
   $g = Group::getByName('students');
   if($u->inGroup($g)) {
      $("div").show();
   }
   else {
      $("div").hide();
   }
   ?>
   <div id="logo">
            <img src="/img/logo.png" alt="Endowr Logo" />
        </div>

 
mkly replied on at Permalink Best Answer Reply
mkly
Pretty close, but it looks like you are mixing some javascript with php. What you would use is 'if then' but you would put that div inside the 'if then'. There are two ways people typically do this
<?php
  $u = new User();
  $g = Group::getByName('students');
?>
<?php if($u->inGroup($g)): ?>
<div id="logo">
  <img src="/img/logo.png" alt="Endowr Logo />
</div>
<?php endif; ?>


or

<?php
  $u = new User();
  $g = Group::getByName('students');
  if($u->inGroup($g)) {
?>
<div id="logo">
  <img src="/img/logo.png" alt="Endowr Logo />
</div>
<?php } ?>


The thing to note in either of those is that anything outside of the <?php ?> stuff is just outputted to the browser as is. Typically that is html code like above.

$("div").show();

Is actually some jQuery(a javascript library code). In jQuery's case the dollar sign is a shorcut for writing jQuery("div").show(); whereas in php's case a dollar sign simply defines a string of text as a variable.