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();
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 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
}
I have a problem with the Laravel 5 Validation, I post data with the Angular method $http.post. I have allready created a Request file called, CreateProjectRequest.
The posted data looks like this.
Array(
[0] => Array
(
[project_id] =>
[title] => test
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => 'test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
[1] => Array
(
[project_id] =>
[title] => test2
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
)
I need to validate title and tasks, how can I reach that?
I tried a few things but the I get each time a error. For example title is required while title was filled in.
// Empty rules array
$rules = [];
foreach ($this->request->all() as $keyGroup => $request)
{
$rules['title'] = 'required';
// Loop throug tasks
foreach ($request['tasks'] as $key => $task)
{
$rules['quantity'] = 'required|numeric';
}
}
return $rules;
current output is: 3 array
Array ( [pf_id] => 5 [pf_title] => Gender [pf_icon] => [pf_key] => gender )
Array ( [pf_id] => 6 [pf_title] => Location [pf_icon] => [pf_key] => location )
Array ( [pf_id] => 7 [pf_title] => Interests [pf_icon] => [pf_key] => interests )
the result must be this: 1 array including 1 subarray for each pf_id
Array (
5 => array ([pf_title] => Gender [pf_icon] => [pf_key] => gender)
6 => array ([pf_title] => Location [pf_icon] => [pf_key] => location)
7 => array ([pf_title] => Interests [pf_icon] => [pf_key] => interests)
)
the code for to fetch data:
$cfields = array();
$this->DB->build( array(
'select' => 'pf_d.pf_id, pf_d.pf_title, pf_d.pf_icon, pf_d.pf_key',
'from' => array( 'pfields_data' => 'pf_d' ),
'where' => 'pf_d.pf_id = 5 or pf_d.pf_id = 6 or pf_d.pf_id = 7',
) );
// if ($this->settings['b_multiplayer_f_id'] && in_array( $this->request['f'], explode(',', $this->settings['b_multiplayer_f_id'] ) ) ) {
print $this->DB->fetchSqlString();
// }
$this->DB->execute();
while ( $row = $this->DB->fetch() ) {
// array correction starts please help!!
# $extract = array();
# $extract = implode/explode??
# $extract[] = $row['pf_id'].' => '.array( 'pf_title' => $row['pf_title'], 'pf_icon' => $row['pf_icon'], 'pf_key' => $row['pf_key']);
// array correction finish
$cfields[] = $extract;
print_r($row);
}
i am using invisionpower board (i am customer but it not require a their response... to solve)
and how I fetch data array( n => array()) into template?
exemple code:
<foreach loop="$cfields as $key=>$value">
{$value[pf_title]} => {$this->memberData['field_'.$key]}
</foreach>
is correct?
i have solved... I edit while() into
while ( $row = $this->DB->fetch() ) {
$b_array = array_slice($row, 1);
$cfields[ $row['pf_id'] ] = $b_array;
}
result
Array
(
[5] => Array
(
[pf_title] => Gender
[pf_icon] =>
[pf_key] => gender
)
[6] => Array
(
[pf_title] => Location
[pf_icon] =>
[pf_key] => location
)
[7] => Array
(
[pf_title] => Interests
[pf_icon] =>
[pf_key] => interests
)
)
Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )