循环通知每个用户

时间:2018-01-05 08:23:37

标签: php arrays function loops

我正在尝试向分配给项目的每个用户发送通知。 我可以通过这样做获得所有用户ID

$ids = array_column($users, 'id');
    $userid = implode(', ', $ids);
    echo $userid;

但是当我试图把它变成insida时,我的功能没有发生。它只是空白。 我也尝试在我的函数中移动“获取用户脚本”,但仍然没有运气。

修改

我也尝试手动编写$ids = array("14","1");但是我得到了同样的错误。但是如果我把它移到它的功能里面就可以了!如果我对$ids = array_column($users, 'id');执行相同操作,则会返回错误

如果我这样做,它会起作用。但我想通过自动获取用户。

$ids = array(
  array(
    'id' => 1,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 14,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);


    foreach ($ids as $key => $user) {
        $um_notifications->api->store_notification( $user['id'], 'new_action', $vars );
   }

这是完整的脚本,我希望我已经解释得很好。

<?php


    /* #### TEST OUTSIDE THE FUNCTION 'trigger_new_notification' #### 
        THIS WORKS AND PRINTING EX: '1, 8, 13'
    */ 
    $ids = array_column($users, 'id');
    $userid = implode(', ', $ids);
    echo $userid;



    /* #### NOTIFICATION 'trigger_new_notification' START #### */

    add_action('um_before_profile_fields', 'trigger_new_notification', 100);
    function trigger_new_notification( $args ) {
        global $um_notifications;


    /* Get information about the project */
    $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );  

    /* Send notification to every assaigned user */
    foreach($ids as $row)
        {
            $um_notifications->api->store_notification( $row, 'new_action', $vars );
        }


     /* How it should print, where '14' is the user id */
    /* $um_notifications->api->store_notification( 14, 'new_action', $vars ); */


    }

    do_action( 'um_before_profile_fields');

    ?>

4 个答案:

答案 0 :(得分:0)

我使用一个$ id作为函数的参数,你可以试试这个:

 jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=utf-8 

如果您尝试使用ids av argumt数组,您可以尝试以下代码:

function trigger_new_notification( $id ) {
        global $um_notifications;
        $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );  
  $um_notifications->api->store_notification( $id, 'new_action', $vars );

   }

答案 1 :(得分:0)

@Andreas我同意你对$ args的看法。我从http://docs.ultimatemember.com/article/53-using-notifications-api-to-add-custom-notifications

获得了脚本

这是对的吗?同样的问题,但var_dump现在说 string(0)“”。

$ids = array_column($users, 'id');


/* #### NOTIFICATION ACTIONS #### */            
add_action('um_before_profile_fields', 'trigger_new_notification', 100);
function trigger_new_notification( $ids ) {
        global $um_notifications;

                um_fetch_user( get_current_user_id() );


/* Get information about the project */
$vars = array(
    "post_title" => get_post_field( 'post_title', GetLastPostId() ),
    "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
    "member" => um_user('display_name'),
    "notification_uri" => '/projects/?project_id='.GetLastPostId().'&tab=project&action=index'
);  

$um_notifications->api->store_notification( $ids, 'new_action', $vars );


}

do_action( 'um_before_profile_fields');

答案 2 :(得分:0)

从一些试验和错误中找到了解决方案。

不确定使其起作用的单点是什么,但所做的更改是:
添加了一个功能调用
为功能调用添加了$ ID 在函数声明中添加了$ id

仍然不确定$ args是什么,但我把它留在了那里。

$ids = array_column($users, 'id'); // get IDs of users
add_action('um_before_profile_fields', 'trigger_new_notification', 100); 

function trigger_new_notification( $args , $ids ) {
    global $um_notifications;
    $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );
    foreach($ids as $row){ // iterate the userIDs
            $um_notifications->api->store_notification( $row, 'new_action', $vars );
    }
}

do_action( 'um_before_profile_fields'); 


trigger_new_notification( $args , $ids ); // Call function and pass $args and $ids as variables

答案 3 :(得分:0)

对于仍在寻求帮助的人

$um_notifications->api->store_notification(

已更改为:

UM()->Notifications_API()->api()->store_notification(