CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Is this how you find if a contact is in a group?
Pages: [1]

Author Topic: Is this how you find if a contact is in a group?  (Read 1245 times)

vanalive

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
Is this how you find if a contact is in a group?
November 09, 2008, 03:51:20 pm
I'm trying to find if a contact is in group_ID 36.

---

if (module_exists('civicrm')) {
global $user;
civicrm_initialize(true);
require_once 'api/v2/Contact.php';
require_once 'api/v2/GroupContact.php';
require_once 'api/UFGroup.php';
$userID = crm_uf_get_match_id( $user->uid );
$params = array( 'contact_id' => $userID);
$contact = civicrm_group_contact_get(&$params );
$groupID = "36";
if (in_array($groupID, $contact, true)) {
echo "You are in Group 36";
}
?>


My in_array isn't returning true. When I run print_r($contact), I get this:

[3433] => Array
(
[id] => 3433
[group_id] => 36
[title] => Phone Canvass Access
[visibility] => User and User Admin Only
[in_date] => 2008-11-07 15:47:44
[in_method] => Admin
)

All I want to do is check if my user is in a specific CiviCRM group.
« Last Edit: November 09, 2008, 03:56:36 pm by vanalive »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Is this how you find if a contact is in a group?
November 09, 2008, 04:47:35 pm

you might want to check the php manual (http://www.php.net/manual/en) to get a better idea on how to do this

rough code for what u need is:

Code: [Select]
$inGroup = false;
foreach ( $contact as $contactID => $group ) {
  if ( $group['group_id'] == $groupID ) {
     $inGroup = true;
     break;
  }
}



lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

vanalive

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
Re: Is this how you find if a contact is in a group?
November 09, 2008, 09:03:35 pm
Thanks!  Works great.

<?php
if (module_exists('civicrm')) {
global $user;
civicrm_initialize(true);
require_once 'api/v2/Contact.php';
require_once 'api/v2/GroupContact.php';
require_once 'api/UFGroup.php';
$userID = crm_uf_get_match_id( $user->uid );
$params = array( 'contact_id' => $userID);
$contact = civicrm_group_contact_get(&$params );
$groupID = '36';
$inGroup = false;
foreach ( $contact as $contactID => $group ) {
  if ( $group['group_id'] == $groupID ) {
     $inGroup = true;
     break;
  }
}

}
?>

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Is this how you find if a contact is in a group?

This forum was archived on 2017-11-26.