I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.
Here is my code:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_name = $user->user_email;
$user_email = $user->display_name;
endforeach;
So I can then use them in other areas of the page, ie:.
echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;
I've printed out the $users array which contains the following:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 46
[user_login] => huw
[user_pass] => u7CqxbuQDvApUAF6tT.
[user_nicename] => huw
[user_email] => huw#xxxx.co.uk
[user_url] =>
[user_registered] => 2017-02-06 11:13:09
[user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
[user_status] => 0
[display_name] => Huw Daniel Rowlands
)
[ID] => 46
[caps] => Array
(
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => sssg
[1] => new_role
[2] => site_member
[3] => test_role
)
[allcaps] => Array
(
[read] => 1
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 308
[user_login] => jeremy
[user_pass] => LLOKbkPOWQsBKUIk2qL1
[user_nicename] => magnus
[user_email] => jeremy#gmail.com
[user_url] =>
[user_registered] => 2017-05-03 19:24:42
[user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
[user_status] => 0
[display_name] => Jeremy
)
[ID] => 308
[caps] => Array
(
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => new_role
[1] => sssg
[2] => site_member
)
[allcaps] => Array
(
[read] => 1
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[filter] =>
)
)
If you want to send an email to all users with a certain role, try something like this:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email = $user->user_email;
$user_name = $user->display_name;
send_email($user_name, $user_email);
endforeach;
And then write your email function:
function send_email($name, $email) {
// Do email sending stuff here with $name & $email
}
Related
I have the following stdClass Object contained within $response:
stdClass Object ( [domain] => stdClass Object ( [id] => d1111111 [spamscore] => 75 [rejectscore] => 200 ) [domainalias] => Array ( ) [wildcard] => Array ( ) [catchall] => Array ( ) [forward] => Array ( ) [mailbox] => Array (
[0] => stdClass Object ( [highEmailNotification] => [id] => m1111111 [lastPasswordChange] => 2020-02-19T22:41:12+00:00 [local] => mailbox1 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[1] => stdClass Object ( [highEmailNotification] => [id] => m2222222 [lastPasswordChange] => 2020-02-17T15:46:21+00:00 [local] => mailbox2 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[2] => stdClass Object ( [highEmailNotification] => [id] => m3333333 [lastPasswordChange] => 2020-02-19T15:00:36+00:00 [local] => mailbox3 [lowEmailNotification] => [quotaMB] => 1024 [receive] => 1 [rejectscore] => 0 [send] => 1 [spamscore] => 75 [usageMB] => 0 [enabled] => 1 ) ) [spamblacklist] => Array ( ) [spamwhitelist] => Array ( ) [responder] => Array ( ) [name] => domain.com )
I need to convert it into an array and extract particular values, i.e. [id] and [local] from it.
Speed is also an issue, as this array will grow to thousands of items, so if there is other, quicker way than 'foreach' it would be better.
I used some suggestions from here, such as:
$array = json_decode(json_encode($response), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
and got partial success with the results:
d1111111 -
-
-
-
-
i - i
(so it found the very first [id] value)
it however missed the most important values I am after.
What I need to get is:
m1111111 - mailbox1
m2222222 - mailbox2
m3333333 - mailbox3
Any suggestions are greatly appreciated.
The values you're trying to print is in a nested array within $response.
Try this.
$array = json_decode(json_encode($response['mailbox']), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
I have two Arrays 1st Array give me a role form Roles table and second array give me the data using this role_id key.
When i pass the array to view how can i get Role that is from first foreach().
This is my
SettingController.php
public function index()
{
$users = User::with('roles')->get()->all();
$access_names = AccessName::all();
$modules = Module::all();
$entries = Entry::all();
$general_settings = Setting::all()->first();
$module_permissions = ModulePermission::get()->toArray();
$roles = Role::get();
$num_roles = count($roles);
$data = 'roles';
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
}
}
}
return view('backend.setting.index', compact(['general_settings', 'roles', 'users', 'access_names', 'roles_data','modules','entries']));
}
My output of this code is like this
Array
(
[Admin] => Array
(
[1] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
If i can access data from this then very well other wise i make a new array like this from i can access this by a new array that is data so it is easy to check that
I also tried this but this is not working properly
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
$array['data'] = $roles_data;
}
}
}
But If output come like this that is also very good
Array
(
[data]=>array
(
[Admin] => Array
(
[0] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
)
In view i want to do like this.
view.blade.php
if($role->name == 'data['Admin']'){
//then do this
}
elseif($role->name == 'data['User']'){
// do this
}
else{
// do this
}
And i also want to this dynamic what can i do to make dynamic i not use 'admin' and 'user' because if i have many roles it is difficult to handle.
Thanks.................
function export(){
ini_set('memory_limit', '256M');
$this->loadModel('Profile');
$this->loadModel('ExamPayment');
//$this->loadModel('ExamApplication');
//$clemrep = $this->ExamApplication->find('all');
$ufacid = Authcomponent::user('faculty_id');
$options = array( 'conditions' => array('ExamPayment.status' => "Successful",'ExamPayment.faculty_id'=> $ufacid) );
$this->ExamPayment->Behaviors->load('Containable');
$this->ExamPayment->contain(
array('User'=>array('Profile'),'Exam','Faculty','ExamApplication')
);
$examApps = $this->ExamPayment->find('all', $options, array('contain'=>
'ExamApplication'
));
$examApplications = array();
foreach ($examApps as $key => $examApplication) {
$fname = isset($examApplication['User']['Profile']['fname'])? trim($examApplication['User']['Profile']['fname'] ):null;
$lname = isset($examApplication['User']['Profile']['lname'])? trim($examApplication['User']['Profile']['lname'] ):null;
$phone = isset($examApplication['User']['Profile']['phone'])? trim($examApplication['User']['Profile']['phone'] ):null;
$gender = isset($examApplication['User']['Profile']['gender'])? trim($examApplication['User']['Profile']['gender'] ):null;
$nationality = isset($examApplication['User']['Profile']['nationality'])? trim($examApplication['User']['Profile']['nationality'] ):null;
//$adddress = isset($examApplication['User']['Profile']['adddress'])? trim($examApplication['User']['Profile']['adddress'] ):null;
//$dob = isset($examApplication['User']['Profile']['dob'])? trim($examApplication['User']['Profile']['dob'] ):null;
$examApplications[] = array(
'ExamApplication' => array(
'full_name' => $fname . ' ' . $lname,
'email' => $examApplication['User']['email'],
'phone' => $phone,
//'adddress' => $adddress,
//'date_of_birth' => $dob,
'gender' => $examApplication['User']['Profile']['gender'],
'nationality' => $examApplication['User']['Profile']['nationality'],
// 'state' => $examApplication['User']['Profile']['state'],
//'ref_number' => $examApplication['ExamApplication']['ref_number'],
//'role' => $examApplication['User']['role'],
'exam' => $examApplication['Exam']['exam'],
//'init' => $examApplication['Exam']['init'],
'code' => $examApplication['Faculty']['code'],
'med_school' => $examApplication['ExamApplication']['med_school'],
'med_school_address' => $examApplication['ExamApplication']['med_school_address'],
'med_school_year' => $examApplication['ExamApplication']['med_school_year'],
'med_board_year' => $examApplication['ExamApplication']['med_board_year'],
'discharge_year' => $examApplication['ExamApplication']['discharge_year'],
'discharge_type' => $examApplication['ExamApplication']['discharge_type'],
'exam_date' => $examApplication['ExamApplication']['exam_date'],
'exam_center' => $examApplication['ExamApplication']['exam_center'],
//'exam_fee' => $examApplication['ExamPayment']['exam_fee'],
'status' => $examApplication['ExamPayment']['status'],
)
);
}
//pr($clemrep);
pr($examApplications);
exit();
$this->set(compact('examApplications'));
$this->layout = null;
$this->autoLayout = false;
Configure::write('debug','0');
}[attached is the output i get][1]
Below is the ouput of the array, my question is, why are some value blank?
Array
(
[0] => Array
(
[ExamApplication] => Array
(
[full_name] => FRANK CHIDI NWANKPA
[email] => franknwankpa#gmail.com
[phone] => 07030940000
[gender] => Male
[nationality] => NG
[exam] => Primary
[code] => 205
[med_school] =>
[med_school_address] =>
[med_school_year] =>
[med_board_year] =>
[discharge_year] =>
[discharge_type] =>
[exam_date] =>
[exam_center] =>
[status] => Successful
)
)
[1] => Array
(
[ExamApplication] => Array
(
[full_name] => BANKOLE ARAFAT ABOSEDE
[email] => bankolearafat#gmail.com
[phone] => 08062683798
[gender] => Female
[nationality] => NG
[exam] => Primary
[code] => 205
[med_school] =>
[med_school_address] =>
[med_school_year] =>
[med_board_year] =>
[discharge_year] =>
[discharge_type] =>
[exam_date] =>
[exam_center] =>
[status] => Successful
)
)
[2] => Array
(
[ExamApplication] => Array
(
[full_name] => Uvieroghene Ogbebor
[email] => uvieo#yahoo.com
[phone] => 08038975401
[gender] => Male
[nationality] => NG
[exam] => Primary
[code] => 205
[med_school] =>
[med_school_address] =>
[med_school_year] =>
[med_board_year] =>
[discharge_year] =>
[discharge_type] =>
[exam_date] =>
[exam_center] =>
[status] => Successful
)
)
why are some values blank, i found out that the content of the "ExamApplication" model is not being displayed , however, the model has already been loaded.
What could be happening?
Please help me
I am trying to add the data of same field and want to return a result i used the following query:
$total = $this->Details->find('all', array(
'fields' => array('sum(Details.total_downtime+ Details.total_downtime)'),
'conditions' => array('Details.site_id' => $id)
));
print_r($total->toArray());
exit;
And I am getting the following result:
Array (
[0] => App\Model\Entity\Detail Object (
[displayField] => username
[_accessible:protected] => Array (
[*] => 1
[id] => 1
[site_id] => 1
[uptime] => 1
[downtime] => 1
)
[_properties:protected] => Array (
[sum(Details] => Array ( [total_downtime+ Details] => 4 )
)
[_original:protected] => Array ( )
[_hidden:protected] => Array ( )
[_virtual:protected] => Array ( )
[_className:protected] => App\Model\Entity\Detail [_dirty:protected] => Array ( )
[_new:protected] =>
[_errors:protected] => Array ( )
[_registryAlias:protected] => Details
)
)
Where can I find my sum?
I guess you are trying to achieve something like this?
$query = $Details->find();
$query
->select(['sum' => $query->func()->sum('Details.total_downtime')])
->where(['Details.site_id' => $id])
->toArray();
You can do this collection object like below:
$Details = $this->Details->find();
$Details = $Details->where(['Details.site_id' => $id]);
$sumOftotal_downtime = $Details->sumOf('Details.total_downtime');
Cookbook Reference
Try this:
$Details = this->Details->find()->select(['sum'=>'SUM(Details.total_downtime)'])->where(['Details.site_id' => $id])->toArray();
i have two arrays and i want two merge using the same value within each array..
the first array is :
[0] => Array
(
[id] => 1
[uid] => 0090000157
[cid] => 0090000007
[extension] => 202
[secret] => Myojyo42f!
[leader] => 1
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 12:20:20
[updated_at] => 2015-07-26 12:20:20
)
[1] => Array
(
[id] => 2
[uid] => 0090000159
[cid] => 0090000007
[extension] =>
[secret] => Myojyo42f!
[leader] =>
[simultaneous] =>
[confbridge_id] => 2
[created_at] => 2015-07-26 14:23:41
[updated_at] => 2015-07-26 14:23:41
)
)
and the second array is:
Array
(
[0] => Array
(
[_id] => 55b52f4c2bab38fc63b6272a
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001b
[uniqueid] => 1437937478.63
[conference] => 0090000156
[calleridnum] => 0090000157
[calleridname] => 0090000157
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 202
[extentype] => 0
[extenrealname] =>
[name] => 0090000157
[secret] => Myojyo42f!
[username] => 0090000157
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
[1] => Array
(
[_id] => 55b53a2e2bab38fc63b6272b
[event] => ConfbridgeJoin
[channel] => SIP/peer_voip301confbridge-0000001c
[uniqueid] => 1437940260.66
[conference] => 0090000156
[calleridnum] => 0090000158
[calleridname] => UID2
[__v] => 0
[sipSetting] => Array
(
[accountcode] =>
[accountcode_naisen] => 203
[extentype] => 0
[extenrealname] =>
[name] => 0090000158
[secret] => Myojyo42f!
[username] => 0090000158
[context] => innercall_xdigit
[gid] => 101
[cid] => 0090000007
)
)
)
i want to merge array with the same value for example :
first array has = [uid] => 0090000157
second array has = [calleridnum] => 0090000157
is it possible to merge them?
this is my code
{foreach from=$participants item=participant key=p }
{foreach from=$conference_participants item=conference_participant key=c}
{if$participants.calleridnum == $conference_participants.uid}
//how to get data here ?
{/if}
{/foreach}
{/foreach}
Could this be what you're looking for?
Sorry for all the changes, I should have tested it... grr
(I've put this in PHPFiddle:
<pre>
<?php
$participants = [
[ 'calleridnum' => 1,
'test' => 'yay'
]
];
$conferance_participants = [
[ 'uid' => 1,
'test' => 'yay2',
'dit' => 'deze'
]
];
foreach ($participants as $participant=>$p) {
foreach ($conferance_participants as $conferance_participant=>$c) {
if ($p['calleridnum'] == $c['uid']) {
// ID's match do the magic here
foreach ( $c as $key=>$val ) {
if (!isset($p[$key])) {
// Value is new, copy the conferance_participant to the participant
$participants[$participant][$key] = $val;
}
} // Merging data
} // If: Match
}
}
print_r( $participants );
?>
)