I created a dropdown-menu named title in my profile2-module (in combination with taxonomy) and use it in the registration form.
How can I best determine the user-selected value?

//The following 3 lines of code resulted in the following:
	//echo '<pre>';
	//print_r($profile);
	//echo  '</pre>';

[field_title] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [tid] => 2
                            [taxonomy_term] => stdClass Object
                                (
                                    [tid] => 2
                                    [vid] => 2
                                    [name] => Mr
                                    [description] => 
                                    [format] => plain_text
                                    [weight] => 0
                                    [vocabulary_machine_name] => title
                                )

                        )

                )

        )

The following lines of code work quite well, however there might be a simpler way.

	// load profile fields
	global $user;
	$uid = user_load($user->uid);
	
	//Load 'Personal'-Fields
	$profile = profile2_load_by_user($uid, 'personal');
	$title = $profile->field_title['und'][0]['tid'];
	$sql = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid = :title', array(':title' => $title));
	$result = $sql->fetch();
	if (!empty($result)) { $title = $result->name; }

Any suggestions?
Thank you!

Comments

Nikdilis’s picture

Anyone with a better solution?

Nikdilis’s picture

Status: Active » Closed (fixed)

Solved:
See: http://drupal.org/node/1323842

$title = $profile->field_title['und'][0]['taxonomy_term']->name;
no mysql-query needed.

faqing’s picture

Thank you, Nikdilis
The long coding your provided works great. But the short one does not.
Anything wrong:

$title = $profile->field_title['und'][0]['taxonomy_term']->name;
print $title;

I need you further help:
If I want to display a node reference (link to another node title) not taxonomy term, what I should do?

print ($profile->field_course['und'][0]['nid']);
The above code only displays the node number. I want to display the title.

Nikdilis’s picture

Here's an example of getting values of text-fields and names of taxonomy-fields in Drupal 7.
This is not related to Profile2 but may be helpful nevertheless!!!!

<?php
//Gets a loaded object from a router item => http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_get_object/7
	$node = menu_get_object();

//Define variables:
	//Get the value of text-fields:
	$description = check_plain($node->field_description['und'][0]['value']);
	$gid= check_plain($node->field_gid['und'][0]['value']);

	//Get the name of a taxonomy-field:
	$tid_category = check_plain($node->field_category['und'][0]['tid']);
	$term_category = taxonomy_term_load($tid_category);
	$termname_category = $term_category->name;

//Output variables:
	print $description. '<br>';
	print $gid . '<br>';
	print $termname_category . '<br>';
?>
Nikdilis’s picture

Issue summary: View changes

none