2009-08-18 25 views
2

如何使用profile_save_profile?Drupal 6:以编程方式更改用户值

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??? 
} 

我在做什么错?

编辑:这也将失败:

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

profile_save_profile($edit, user_load(3), 'Fencing Information', TRUE); 
+0

是发生在$计算编辑分配线工作正常(没有除零或类似的东西)?你能分享错误讯息吗? – 2009-08-18 12:44:07

+0

计算打印0和1之间的数字。没有错误消息。它曾经说“你只能通过引用传递变量”,我通过直接使用变量$ edit而不是array()来解决这个问题。 – 2009-08-18 16:07:09

回答

3

我认为问题是,你指定$register(最后一个参数)作为TRUE。这仅在创建新帐户时使用,因此如果设置,您将只能保存注册页面上可用的配置文件字段,这可能不是您想要的。

由于它不是必需的参数,所以您可以将其忽略。

当涉及到编辑的格式,它预计相同的格式$form_state['values']你将不得不如果其中从表单来的,例如值:

<?php 
$edit = array(
    'fencing_style' => 'Aggressive', 
    'favorite_weapon' => 'sabre', 
    'left_handed' => FALSE, 
); 
profile_save_profile($edit, $user, 'Fencing Information'); 
+0

yessss谢谢 – 2009-08-18 18:50:18

+1

这是否意味着'击剑信息“类别设置为$ edit,覆盖其他值(可能不会键入$编辑)?我有两个单独的函数,它们在”防护信息“中使用不同的值。运行时,它将删除由其他 – 2009-08-19 06:38:20

+1

是的,profile_save_profile()覆盖了所有现有的值 - 不是很优雅:http://api.drupal.org/api/function/profile_save_profile/6 配置文件模块是Drupal API的一个较黑暗的角落,而且它已经在Drupal 7中完全重制 - 所以它将更容易处理 - 灵活性和可扩展性。 – mikl 2009-08-19 13:33:11

0

在profile_save_profile功能看,它看起来像它预期$用户中,而不是用户$传递> UID - 所以尝试修改您的来电如下:

profile_save_profile($edit, $user, 'Fencing Info', TRUE); 
+0

我这样做了,把'Fencing Info'改为'Fencing Information'(正确的类别),它仍然不起作用,$ edit必须在什么格式? – 2009-08-18 17:55:12

相关问题