Forums

Show some fields only to particular user groups

growlcat 11 May, 2011
I think there must be a fairly simple answer to this:
I want to be able to show some of the fields from my database to the general public, and some extra fields only to logged in users.
I guess I need to use php but can anyone give me an example?
Thanks very much!
GreyHead 12 May, 2011
Hi growlcat,

You can add conditional code in the Body section; first in the Header add
<?php
$user =& JFactory::getUser();
global $is_admin;
$is_admin = false;
if ( in_array($user->gid, array('24', '25')) ) {
  $is_admin = true;
}
?>

Then in the Body section
<?php
global $is_admin;
?>
. . .
<?php if ( $is_admin ) { ?>
// some code for admins only
<?php } ?>
. . .
<?php if ( !$is_admin ) { ?>
// some code for non-admins only
<?php } ?>


Bob
growlcat 13 May, 2011
Thanks so much for your help Bob,
I am getting an error message in the header code:
Parse error: syntax error, unexpected '{' in /....../components/com_chronoconnectivity/libraries/connection.php(246) : eval()'d code on line 5
The body code works to remove the fields I've selected from public view, but now even super admin cannot view them! Is this because of the problem in the header code?
How can I change the code so that all logged in users can see the extra fields?
Thanks again
growlcat 13 May, 2011
Back again! I still can't figure out what is causing the parsing error, but have found the list of Joomla group IDs as follows:
Joomla Group ID - Joomla User Type
29 - Public Frontend
18 - Registered
19 - Author
20 - Editor
21 - Publisher
30 - Public Backend
23 - Manager
24 - Administrator
25 - Super Administrator

So if I can get Bob's code to work, I would need to add everything but group 29 to the array. This should allow all logged in users to access the extra fields.
All help with this is greatly appreciated!
GreyHead 13 May, 2011
Hi growlcat,

It looks like I missed a ) in this line
if ( in_array($user->gid, array('24', '25') ) {
it should be
if ( in_array($user->gid, array('24', '25')) ) {


I'll fix my original post.

Looged in users include Group 29 - I've never seen that used though. The default Joomla! registration Group is 18 and anything else needs to be assigned. Unless you are using Group 29 you can test for logged in users with
if ( $user_id ) {
This works because an un-logged in visitor is assigned an id of 0.

Bob
growlcat 13 May, 2011
Thanks Greyhead, I think that's one too many ')' now.
I get the following error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in .../components/com_chronoconnectivity/libraries/connection.php(246) : eval()'d code on line 5
GreyHead 13 May, 2011
Hi growlcat,

Too early in the morning to be sure but three ((( and three ))) looks correct.

Bob
growlcat 13 May, 2011
Thanks so much Bob, it's working perfectly now.
Just for anyone else who wants to know how to do this, I have a bushwalking program on my website. It lists all the walks chronologically starting from today's date. I don't want it to show meeting details, email addresses or telephone numbers to the general public, but I do want it to show this information to registered users. Here's my body code using the example:
<?php
global $is_admin;

$va = $MyRow->start;
$date = $va;
$date = date('D-j-M-y', strtotime($date));
echo  $date;  
?>
 to 
<?php
$va = $MyRow->end;
$date = $va;
$date = date('D-j-M-y', strtotime($date));
echo  $date;  
?>
    <b>{place}</b><br/>
Grade: {grade}; {distance} km; asc/desc {ascdesc}m.<br/>
{description}<br/>
<b>Leader: {leader}</b>
<?php
if ( $is_admin ) { 
?>
   <a href="mailto:{email}?subject=Enquiry from NPA website about your bushwalk on {start}"><img src="/images/stories/bushwalking/email.gif" width="25" height="15" alt="email" /></a>, <img src="/images/stories/bushwalking/phone.gif" width="23" height="19" alt="email" /> {phone}, mobile: {mobile}<br/>
Transport details: {transport}
<?php 
} 
?>
<br/>
Branch: {branch}<br/>
{edit_record} {delete_record} </p><p>
This topic is locked and no more replies can be posted.