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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Unable to echo fields within an array
Pages: [1]

Author Topic: Unable to echo fields within an array  (Read 2393 times)

slaterino

  • Guest
Unable to echo fields within an array
July 17, 2009, 10:04:57 am
I recently used an API to display details of all groups using various criteria. Now I have tried to do a similar task with Memberships but for some reason it will not display the fields and I have no idea why. When I use print_r it shows all the values in the array, including the values I am wanting to echo, but when I do try and echo them afterwards the results are blank. Why is this? I have tried doing this a number of ways but can't seem to get anything to work. This is my code:

Code: [Select]
    $params = array(
                   'contact_id' => 3
                   );

    $member_groups     =& civicrm_contact_memberships_get($params);

    if ( civicrm_error ( $member_groups )) {
        return $member_groups['error_message'];
    } else {
print_r($member_groups);

foreach($member_groups as $result_group) {
    echo $result_group['membership_name'] . ". " . $result_group['start_date'] . " - " . $result_group['end_date'];
}

    }

The print_r function gives the following, but the echo'd fields produce nothing. Why is this? It really has me confused!

Code: [Select]
Array ( [3] => Array ( [1] => Array ( [id] => 1 [membership_id] => 1 [contact_id] => 3 [membership_contact_id] => 3 [membership_type_id] => 1 [join_date] => 2009-05-13 [start_date] => 2009-05-13 [membership_start_date] => 2009-05-13 [source] => Online Contribution: Personal User Test[membership_source] => Online Contribution: Personal User Test[status_id] => 1 [is_test] => 0 [member_is_test] => 0 [is_pay_later] => 0 [member_is_pay_later] => 0 [membership_name] => Personal User [relationship_name] => Employee of ) ) [record_count] => 1 )

abrookins

  • I’m new here
  • *
  • Posts: 21
  • Karma: 5
    • Redspire (Blog)
Re: Unable to echo fields within an array
July 19, 2009, 10:47:51 am
If you take a closer look at that print_r, you'll see that you are actually getting back an array nested within an array.  Try doing this:

Code: [Select]
foreach($member_groups as $key => $result_group) {
    echo $result_group['membership_name'] . ". " . $result_group['start_date'] . " - " . $result_group['end_date'];
}

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: Unable to echo fields within an array
July 20, 2009, 12:21:09 am
slaterino :

I would recommend using the debug statement as :

Code: [Select]
CRM_Core_Error::debug( 'result_group', $result_group );
hth
-Yashodha
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

slaterino

  • Guest
Re: Unable to echo fields within an array
July 28, 2009, 07:35:14 am
Hi, sorry for the late reply to this help. My laptop has been out of action for the past week! But anyway, I have tried doing as suggested, but still am not able to echo any fields. My code now looks like this:

Code: [Select]
    $params = array(
                   'contact_id' => 3
                   );

    $member_groups     =& civicrm_contact_memberships_get($params);

    if ( civicrm_error ( $member_groups )) {
        return $member_groups['error_message'];
    } else {
print_r($member_groups);

foreach($member_groups as $key => $result_group) {
    echo $result_group['membership_name'] . ". " . $result_group['start_date'] . " - " . $result_group['end_date'];
}

What this echoes is:

Code: [Select]
Array ( [3] => Array ( [1] => Array ( [id] => 1 [membership_id] => 1 [contact_id] => 3 [membership_contact_id] => 3 [membership_type_id] => 1 [join_date] => 2009-05-13 [start_date] => 2009-05-13 [membership_start_date] => 2009-05-13 [source] => Online Contribution: Personal User Registration [membership_source] => Online Contribution: Personal User Registration [status_id] => 1 [is_test] => 0 [member_is_test] => 0 [is_pay_later] => 0 [member_is_pay_later] => 0 [membership_name] => Personal User [relationship_name] => Employee of ) ) [record_count] => 1 ) . - . -
The last part ' . - . - ' is where the values should be echoed but they are not there. How can it print the fields correctly but then not allow for them to be echoed. If anyone could give me some more help on this I would be really grateful!!!

mcsmom

  • I post frequently
  • ***
  • Posts: 266
  • Karma: 23
    • Official Joomla! Book
  • CiviCRM version: 4 and 3.4
  • CMS version: Joomla!
Re: Unable to echo fields within an array
July 31, 2009, 11:50:58 am
This is how i did it in my Joomla module:

function testMembershipsGet()
   {
      $session =& JFactory::getSession();
      $contact_id = $session->get('contact_id');
      $result     =& civicrm_contact_memberships_get( $contact_id );

      echo '<h3>Memberships</h3>';
      echo '<ul>';
      foreach($result AS $result) {
         echo '<li> '.$result['membership_name'] .' Expiration Date: '.$result['membership_end_date'] .' </ li>';
         }
      echo '</ul>';
   }
testMembershipsGet();


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Unable to echo fields within an array

This forum was archived on 2017-11-26.