views:

893

answers:

2

I've been trying to figure this out for quite some time without success.

foreach ($users as $user) { //list of users to act on
 $by_user = views_get_view_result('attendance', 'page_3', array($user->uid)); //view containing info about this user
 $edit = array('profile_attendance_short_term' => substr(count($by_user) / count($general), 0, 5)); //calculation

 profile_save_profile($edit, $user->uid, 'Fencing Information', TRUE); //update user profile???
}

What am I doing wrong?

EDIT: This also fails:

$edit = array('profile_attendance_short_term' => 9001);

profile_save_profile($edit, user_load(3), 'Fencing Information', TRUE);
A: 

Looking at the profile_save_profile function, it looks like it expects $user to be passed in rather than $user->uid -- so try modifying your call as follows:

profile_save_profile($edit, $user, 'Fencing Info', TRUE);
Dan U.
I did that, and changed 'Fencing Info' to 'Fencing Information' (the correct category. It still doesn't work. What form does $edit have to be in?
Rosarch
+2  A: 

I think the problem is that you're specifying $register (the last parameter) as TRUE. That is only used when creating new accounts, so if set, you will only be able to save the profile fields available on the registration page, which is probably not what you want.

Since it's not a required parameter, you can just leave it out.

When it comes to the format of edit, it expects the same format as the $form_state['values'] you would have if the values where coming from a form, for example:

<?php
$edit = array(
  'fencing_style' => 'Aggressive',
  'favorite_weapon' => 'sabre',
  'left_handed' => FALSE,
);
profile_save_profile($edit, $user, 'Fencing Information');
mikl
yessss thank you.
Rosarch
does this mean that the 'Fencing Information' category is set to $edit, overwriting other values (that may not be keyed in $edit)? I have two separate functions that use different values in 'Fencing Information'. When one runs, it deletes the values set by the other.
Rosarch
Yeah, profile_save_profile() overwrites all the existing values – not very elegant: http://api.drupal.org/api/function/profile_save_profile/6The profile module is one of the darker corners of Drupal's API, and it has been completely remade in Drupal 7 – so it'll be much easier to work with – flexibility and extensibility.
mikl
Ok, thanks. I know this is subjective, but which do you think is better? Pulling all the profile info out so it's all included in $edit, making a bunch of different categories, or combining every function that calls profile_save_profile() into one?
Rosarch
Well, I thinks that's a bit hard to say, not knowing your specific use case. Whatever works best for you :)
mikl