I have two meta key: us_points & agent_id
I need to sum up all the us_points value with agent ID same for each users.
Here is what I tried:
$cuser = wp_get_current_user(); $a_id = $cuser->agent_id;
global $wpdb;
$result = $wpdb->get_col("SELECT sum(meta_value) FROM $wpdb->usermeta WHERE meta_key = 'us_points' AND agent_id = $a_id" );
echo $result[0];
If I remove AND agent_id = $a_id, it shows the sum of all users in the website, but I need to show the sum of only the meta key value common/matching in agent_id
Help will me much appreciated.
Try this, It should target agent_id meta key then group records by the same agent_id value before summing.
$result = $wpdb->get_col("SELECT sum(meta_value) FROM $wpdb->usermeta WHERE meta_key = 'us_points' GROUP BY meta_value HAVING 'meta_key' = 'agent_id'" );
$users = get_users();
$agent_points = array();
foreach( $users as $user ) {
$agent_points[] = array(
'agent_id' => $user->agent_id,
'us_points' => $user->us_points,
);
}
$agent_points expected to be in the structure:
Array
(
[0] => Array
(
[agent_id] => 1
[us_points] => 30
)
[1] => Array
(
[agent_id] => 4
[us_points] => 10
)
[2] => Array
(
[agent_id] => 4
[us_points] => 30
)
[3] => Array
(
[agent_id] => 1
[us_points] => 20
)
)
Then,
$results= array();
foreach( $agent_points as $values ) {
foreach( $values as $key => $value ) {
if( 'agent_id' === $key ) {
$results[ $value ] += $values['us_points'];
}
}
}
Expected $results in agent_id => us_points pair:
Array
(
[1] => 50
[4] => 40
)
Related
I could use some help with storing an array. The process defined below is working and saving a record! It saves the record defined in the [0] column. If a zero is there, it saves record one, if a 1 is there it saves record two etc.
Now I need to automate this so it will do that for EACH RECORD as it iterates through. Perhaps a second foreach statement to tie them together? Or a for loop with an integer variable? I have tried many combos and it is still not working.
//0 = feeobject row[]: to keyfield=>val
//Array ([0] => 0)
foreach($AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0] as $key=>$data ) {
$PaymentID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['PaymentID'];
$ClientID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['ClientID'];
$ClientName = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['ClientName'];
$Account_ID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['Account_ID'];
$State = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['State'];
$PaymentDate = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['PaymentDate'];
$Amount = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['Amount'];
$CheckID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['CheckID'];
$CreditorName = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['CreditorName'];
$DRC_ClientID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['DRC_ClientID'];
$DRC_TransactionID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['DRC_TransactionID'];
$sql = "INSERT INTO tblAgencyFees (PaymentID, ClientID, ClientName, Account_ID, State, PaymentDate, Amount, CheckID, CreditorName, DRC_ClientID, DRC_TransactionID) VALUES ('".$PaymentID."', '".$ClientID."', '".$ClientName."', '".$Account_ID."', '".$State."', '".$PaymentDate."', '".$Amount."', '".$CheckID."', '".$CreditorName."', '".$DRC_ClientID."', '".$DRC_TransactionID."')";
mysqli_query($connection, $sql);
}
//}
//}
echo $sql;
$res = mysqli_query($connection, $sql);
If( !$res ) exit( mysqli_error($connection) );
The array:
Array (
[AgencyFeesGetInfoResult] => Array (
[FeeObject] => Array (
[0] => Array (
[PaymentID] => 816
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-07-31T00:00:00
[Amount] => 8.0000
[CheckID] => 10
[CreditorName] => 0
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
[1] => Array (
[PaymentID] => 817
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-07-31T00:00:00
[Amount] => 92.0000
[CheckID] => 11
[CreditorName] => MF
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
[2] => Array (
[PaymentID] => 847
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-08-14T13:21:49.23
[Amount] => 195.0000
[CheckID] => 13
[CreditorName] => MF
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
)
)
)
You can do a simple loop over your data and do multiple inserts.
$arrFeeObjects = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'];
if(count($arrFeeObjects) > 0)
{
foreach($arrFeeObjects as $intKey => $data)
{
$sql = "INSERT INTO tblAgencyFees (PaymentID, ClientID, ClientName, Account_ID, State, PaymentDate, Amount, CheckID, CreditorName, DRC_ClientID, DRC_TransactionID)
VALUES ('". $data['PaymentID'] ."', '". $data['ClientID'] ."', '". $data['ClientName'] ."', '". $data['Account_ID'] ."', '". $data['State'] ."', '". $data['PaymentDate'] ."', '". $data['Amount'] ."', '". $data['CheckID'] ."', '". $data['CreditorName'] ."', '". $data['DRC_ClientID'] ."', '". $data['DRC_TransactionID'] ."')";
mysqli_query($connection, $sql);
}
}
I am trying to add an array of meta_values to a single meta_key and display them in a list like so:
Assessment One, Passed
Assesment Two, Passed
My code for updating/adding the user_meta:
$user_id = $current_user->ID;
$key = 'assessment_result';
$assessment_name = get_the_title();
$new_value = array($assessment_name, 'Passed');
$meta = get_user_meta($user_id, $key, false);
if ( ! array($meta) ) {
$meta = array();
}
$meta[] = $new_value;
update_user_meta($user_id, $key, $meta, $unique);
After completing two assessments, my array looks like this:
Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => Assessment Two [1] => Passed ) ) [1] => Array ( [0] => Assessment One [1] => Passed ) ) ) 1
There seems to be too many arrays!
Then my query looks like this:
$user_assessment_results = get_user_meta($user_id, 'assessment_result', true);
echo '<ul>';
foreach ($user_assessment_results as $user_assessment_result) {
echo '<li>';
$output = array();
foreach ($user_assessment_result as $result_item) {
$output[] = $result_item;
}
if( count( $output ) ) echo join( ", ", $output );
echo '</li>';
}
echo '</ul>';
Which results in:
Array
Assessment One, Passed
Can you suggest where I am going wrong?
I found the problem $meta = get_user_meta($user_id, $key, false); should have been $meta = get_user_meta($user_id, $key, true);
My code is like this :
My array of city (echo '<pre>';print_r($city);echo '</pre>';die();) :
Array
(
[0] => Array
(
[CityCode] => 14879
[CityName] => Soldeu
)
[1] => Array
(
[CityCode] => 14881
[CityName] => Ari'nsal
)
[2] => Array
(
[CityCode] => 14882
[CityName] => El Tarter
)
[3] => Array
(
[CityCode] => 14883
[CityName] => Grau Roig
)
[4] => Array
(
[CityCode] => 175198
[CityName] => Llorts
)
)
In city code : 14881, city name : Ari'nsal
It's single quote in string.
I try code like this :
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values ";
$valuesArr = array();
foreach($city as $row){
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = mysqli_real_escape_string($row['CityName']);
$created_at = $date;
$updated_at = $date;
$valuesArr[] = "('$nation_code', '$city_code', '$city_name', '$created_at', '$updated_at')";
}
$sql .= implode(',', $valuesArr);
$query = $sql;
$this->db->query($query);
There exist error like this : Message: mysqli_real_escape_string() expects exactly 2 parameters, 1 given....
Any solution to solve my problem?
Thank you very much
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values (?, ?, ?, ?, ?)";
foreach ($city as $row) {
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = $row['CityName'];
$created_at = $date;
$updated_at = $date;
$fields = array($nation_code, $city_code, $city_name, $created_at, $updated_at);
$this->db->query($query, $fields);
}
or
$date = date('Y-m-d H:i:s');
foreach ($city as $row) {
$fields = array(
'nation_code' => $value->nation_code,
'city_code' => $row['CityCode'],
'city_name' => $row['CityName'],
'created_at' = $date,
'updated_at' = $date
);
$this->db->insert('hotel_search_city', $fields);
}
I´m trying to join 2 tables in opencart to make an sales report that specifies taxes (by class, there can be many tax%) for every order. I can only get the values from 1 table, but not 2.
How should I do this?
<?php $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order`, `" . DB_PREFIX . "order_total`");
$ordersr = array();
// Check if there are any rows returned from the query
if ($query->num_rows > 0) {
// Loop through the returned rows for processing
foreach ($query->rows as $result) {
$ordersr[] = array(
'order_id' => $result['order_id'],
'invoice_no' => $result['invoice_no'],
'total' => $result['total'],
'order_status_id' => $result['order_status_id'],
'date_added' => $result['date_added'],
'date_modified' => $result['date_modified'],
'firstname' => $result['firstname'],
'lastname' => $result['lastname'],
'payment_country' => $result['payment_country'],
'payment_method' => $result['payment_method'],
'payment_code' => $result['payment_code'],
'shipping_method' => $result['shipping_method'],
'shipping_code' => $result['shipping_code'],
'currency_code' => $result['currency_code'],
'currency_value' => $result['currency_value'],
'order_status_id' => $result['order_status_id'],
'date_payed' => $result['date_payed']
);
}
} ?>
I am trying to run two model functions from one view. The first query appears to be working fine.
The second query returns a blank array. I noticed when printing the query to the screen that the queries appear to be joining together and are not separate, like this:
JDatabaseQueryMySQL Object ( [db:protected] => JDatabaseMySQL Object ( [name] => mysql [nameQuote:protected] => ` [nullDate:protected] => 0000-00-00 00:00:00 [dbMinimum:protected] => 5.0.4 [_database:JDatabase:private] => Curling_Schedule [connection:protected] => Resource id #19 [count:protected] => 0 [cursor:protected] => Resource id #72 [debug:protected] => [limit:protected] => 0 [log:protected] => Array ( ) [offset:protected] => 0 [sql:protected] => SELECT team_name, division FROM #_autoschedTeams ORDER BY division [tablePrefix:protected] => encex [utf:protected] => 1 [errorNum:protected] => 0 [errorMsg:protected] => [hasQuoted:protected] => [quoted:protected] => Array ( ) ) [type:protected] => select [element:protected] => [select:protected] => JDatabaseQueryElement Object ( [name:protected] => SELECT [elements:protected] => Array ( [0] => sheet, id ) [glue:protected] => , ) [delete:protected] => [update:protected] => [insert:protected] => [from:protected] => JDatabaseQueryElement Object ( [name:protected] => FROM [elements:protected] => Array ( [0] => #__autoschedPlayingSheets ) [glue:protected] => , ) [join:protected] => [set:protected] => [where:protected] => [group:protected] => [having:protected] => [columns:protected] => [values:protected] => [order:protected] => JDatabaseQueryElement Object ( [name:protected] => ORDER BY [elements:protected] => Array ( [0] => sheet ) [glue:protected] => , ) [union:protected] => [autoIncrementField:protected] => )
Model:
public function getTeams()
{
// Create a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Select some fields
$query->select('team_name, division');
// From the hello table
$query->from('#__autoschedTeams');
$query->order('division');
$db->setQuery((string)$query);
$teams = $db->loadResultArray();
$div = $db->loadResultArray(1);
$divs = array_unique($div);
$result = [];
foreach($divs as $key)
{
$result[$key] = [];
foreach(array_keys($div, $key) as $index)
{
array_push($result[$key], $teams[$index]);
}
}
return $result;
}
public function getSheets()
{
// Create a new query object.
$db = JFactory::getDBO();
print_r($query);
$query = $db->getQuery(true);
// Select some fields
$query->select('sheet, id');
// From the hello table
$query->from('#__autoschedPlayingSheets');
$query->order('sheet');
//print_r($query);
$db->setQuery($query);
$result = $db->loadResultArray();
return $result;
}
}
relevant code from View:
$teams_sched = $this->get('Teams');
$sheets_sched = $this->get('Sheets');
Change the name of the variable and check with it.
If you are not getting the result again, then print the query echo $query; and check with it.
public function getSheets()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('sheet, id');
$query->from('#__autoschedPlayingSheets');
$query->order('sheet');
$db->setQuery($query);
$Sheets = $db->loadResultArray();
print_r($Sheets);
return $Sheets;
}