How do i insert multiple rows to a specific postmeta - sql

I am trying update multiple wordpress postmeta fields in one query.
Fields
$fields = [
'token' => $car_hash,
'historie' => $car_history,
'korrekt' => $car_correct,
'kilometer' => $car_km,
'brand' => $car_brand,
'model' => $car_model,
'post_nummer' => $user_zip,
'navn' => $user_name,
'telefon' => $user_phone,
'email' => $user_mail,
'datetime' => $user_datetime,
'customer_city' => $city,
'customer_country' => $country,
'customer_lat' => $lattitude,
'customer_lng' => $longitude
];
Old code which execute a query by using the key value pair in the fields array above.
foreach($fields as $key => $value){
update_field($key, $value, $id);
}
I have been trying to use wpdb::update but i think i am misunderstanding something.
$fields = [
'token' => $car_hash,
'historie' => $car_history,
'korrekt' => $car_correct,
'kilometer' => $car_km,
'brand' => $car_brand,
'model' => $car_model,
'post_nummer' => $user_zip,
'navn' => $user_name,
'telefon' => $user_phone,
'email' => $user_mail,
'datetime' => $user_datetime,
'customer_city' => $city,
'customer_country' => $country,
'customer_lat' => $lattitude,
'customer_lng' => $longitude
];
$tab = 'solgt_postmeta';
$len = count($fields);
$arg = [];
$for = [];
foreach($fields as $key => $value){
$arg[$key] = $value;
$for[] = '%s';
$cnt++;
}
global $wpdb;
if($wpdb->update($tab, $arg, $for)){
echo 'success';
} else {
echo 'error';
}
The formats array look like this
Array
(
[0] => %s
[1] => %s
[2] => %s
[3] => %s
[4] => %s
[5] => %s
[6] => %s
[7] => %s
[8] => %s
[9] => %s
[10] => %s
[11] => %s
[12] => %s
[13] => %s
[14] => %s
);
The arguments array look like this
Array
(
[token] => xxx
[historie] => xxx
[korrekt] => xxx
[kilometer] => xxx
[brand] => xxx
[model] => xxx
[post_nummer] => xxx
[navn] => xxx
[telefon] => xxx
[email] => xxx
[datetime] => xxx
[customer_city] => xxx
[customer_country] => xxx
[customer_lat] => xxx
[customer_lng] => xxx
);
I dont normally use wordpress, and i am confused on how to achieve this without having to do so many queries.

Just use update_post_meta() instead of wpdb::update
<?php
update_post_meta( <post_id>,
'token' , $car_hash,
'historie' , $car_history,
'korrekt' , $car_correct,
'kilometer' , $car_km,
'brand' , $car_brand,
'model' , $car_model,
'post_nummer' , $user_zip,
'navn' , $user_name,
'telefon' , $user_phone,
'email' , $user_mail,
'datetime' , $user_datetime,
'customer_city' , $city,
'customer_country' , $country,
'customer_lat' , $lattitude,
'customer_lng' , $longitude
);

Related

How to sub query a select into another select and put inside a variable with laravel?

I want to make a select that solves the fact that I have to perform two validations one for each table, what i'm doing now is validating the $table1 and validating the $table2 ,i just want to validate $table2
$table1 = DB::table('name_table1')
->where('invoice_id','{$table2}.id')
->get();
$table2 = DB::table('name_table2')
->select('name','invoices','{$table} as itens_of_invoice')
->get();
dd($table);
And return me something like;
Array (
[0] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[1] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[2] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
)
I don't find any solution if someone ever did this please help me

Yii cactivedataprovider count duplicates record and all record count

$criteria = new CDbCriteria;
$criteria->group = 't.blahah_id';
$result = new CActiveDataProvider('SocialnetworkSpams', array(
'criteria' => $criteria,
));
echo "<pre>";
print_r($result->getData());
Array
(
[0] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[created_date] => 2015-12-04 03:50:47
[feedtype] => 1
[feedback] =>
[spam_status] => 1
[approveby_id] => 1
[blahah_id] => 1
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 4
[created_date] => 2015-12-04 04:24:07
[feedtype] => 5
[feedback] => a
[spam_status] => 0
[approveby_id] =>
[blahah_id] => 2
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 4
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
I want duplicate record get using blahah id you can check bellow image:
Thanks
I have been resolved this issue.
$criteria = new CDbCriteria;
$criteria->select = '*, count(*) as total';
$criteria->group = 't.blahah_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
View Page :
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'spam-user-grid',
'itemsCssClass' => 'table table-striped table-bordered table-hover',
'dataProvider' => $model->search(),
'template' => "{items}{pager}",
'pager' => array(
'header' => '',
),
'columns' => array(
array(
'header' => 'Blahah',
'type' => 'raw',
'value' => 'ucfirst($data->ratingprofile->name)',
//'filter' => CHtml::textField('User[name]'),
),
array(
'header' => 'Total',
'type' => 'raw',
'value' => '$data->total',
),
),
)
));

not work scopes in Yii

Why my scopes not work?
$criteria = new \CDbCriteria();
$criteria->addCondition('user_id = :user_id');
$criteria->scopes = array(
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
'filePair' => array(
'with' => array(
'category' => array(
'with' => array(
'parent' => array(
'order' => 'parent.order'
)
),
'scopes' => array('enabled')
),
'file' => array(
'scopes' => array('enabled'),
'order' => 'file.order'
)
)
)
);
$criteria->params = array(':user_id' => \Yii::app()->userApp->id);
/** #var \ApplicantDocument[] $models */
$models = \ApplicantDocument::model()->findAll($criteria);
public function scopes()
{
$t = $this->getTableAlias(false, false);
return array(
'deletedStatus' => array(
'condition' => $t.'.is_deleted = :deletedStatus'
),
'applicant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_APPLICANT)
),
'guarant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_GUARANTOR)
),
'company' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_COMPANY)
),
'trust' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_TRUST)
),
);
}
This scope does not work, aplicants with is_deleted selected from db
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
Thanks for help!
Scopes not working, coz it call in ApplicantDocument, not in applicant. So i am try it solution and it help me.
$criteria->with = array('applicant');
$criteria->together = true;
$criteria->condition = "applicant.is_deleted = 0";

How To Integrate Easypay.pt API With Opengateway

I need to integrate Easypay.pt with opengateway so if any one know somehting about this i would like to know if some one can help for this i will appreciate thanks
function Settings () {
$settings = array();
$settings['name'] = 'Easypay';
$settings['class_name'] = 'easypay';
$settings['external'] = FALSE;
$settings['no_credit_card'] = FALSE;
$settings['description'] =
'Easypay is a portuguese company that offers a universal payment
platform and is certified by SIBS, Unicre, Visa and MasterCard. Our
primary mission is helping the market to shorten the payment
processing time and offer greater flexibility and convenience in
payment.';
$settings['is_preferred'] = 1;
$settings['setup_fee'] = '$0.00';
$settings['monthly_fee'] = '$30.00';
$settings['transaction_fee'] = '2.5% + $0.30';
$settings['purchase_link'] = 'https://www.easypay.pt/_s/api_easypay_01BG.php';
$settings['allows_updates'] = 0;
$settings['url_live'] = 'https://www.easypay.pt/_s/api_easypay_01BG.php ';
$settings['url_test'] = 'http://test.easypay.pt/_s/api_easypay_01BG.php';
$settings['allows_refunds'] = 1;
$settings['requires_customer_information'] = 1;
$settings['requires_customer_ip'] = 1;
$settings['required_fields'] = array(
'enabled',
'mode',
'ep_cin',
'ep_user',
'ep_ref_type',
'ep_entity',
't_key',
'ep_language',
'ep_country'
);
$settings['field_details'] = array(
'enabled' => array(
'text' => 'Enable this gateway?',
'type' => 'radio',
'options' => array(
'1' => 'Enabled',
'0' => 'Disabled'
)
),
'mode' => array(
'text' => 'Mode',
'type' => 'select',
'options' => array(
'live' => 'Live Mode',
'test' => 'Test Mode'
)
),
'ep_cin' => array(
'text' => 'Client Identification Number',
'type' => 'text'
),
'ep_user' => array(
'text' => 'Username',
'type' => 'text'
),
'ep_ref_type' => array(
'text' => 'Type of Identifier',
'type' => 'select',
'options' => array(
'auto' => 'Auto',
)
),
'ep_type' => array(
'text' => 'Type',
'type' => 'select',
'options' => array(
'boleto' => 'Boleto',
)
),
'ep_entity' => array(
'text' => 'Entity in use by Your Account.',
'type' => 'text',
),
't_key' => array(
'text' => 'Transaction key',
'type' => 'text',
),
'ep_language' => array(
'text' => 'Language',
'type' => 'select',
'options' => array(
'PT' => 'PT',
)
),
'ep_country' => array(
'text' => 'Currency',
'type' => 'select',
'options' => array(
'PT' => 'PT',
)
)
);
return $settings;
}
function TestConnection($client_id, $gateway) {
// Get the proper URL
switch($gateway['mode']) {
case 'live':
$post_url = $gateway['url_live'];
break;
case 'test':
$post_url = $gateway['url_test'];
break;
}
$post = array();
$post['ep_cin'] = $gateway['ep_cin'];
$post['ep_user'] = $gateway['ep_user'];
$post['ep_entity'] = $gateway['ep_entity'];
$post['ep_ref_type'] = $gateway['ep_ref_type'];
$post['ep_type'] = 'boleto';
$post['t_value'] = '504.4';
$post['ep_country'] = $gateway['ep_country'];
$post['ep_language'] = $gateway['ep_language'];
$post['s_code'] = 'sssssssssssyour code ddddddd';
$post['t_key'] = $gateway['t_key'];;
$response = $this->Process($post_url, $post);
$status=$response->ep_status;
//$CI =& get_instance();
if($status != 'err1') {
return TRUE;
} else {
return FALSE;
}
}
//--------------------------------------------------------------------
function Process($url, $post, $order_id = FALSE) {
$response = simplexml_load_file(
$url . "ep_cin=" . $post['ep_cin'] . "&ep_user=" . $post['ep_user']
. "&ep_entity=" . $post['ep_entity'] . "&ep_ref_type=" . $post['ep_ref_type']
. "&ep_type=" . $post['ep_type'] . "&ep_country=" . $post['ep_country']
. "&ep_language='" . $post['ep_language'] . "'&s_code=" . $post['s_code']
. "&t_key=" . $post['t_key'] . "&t_value=" . $post['t_value']
);
return $response;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
private function response_to_array($string) {
$string = urldecode($string);
$pairs = explode('&', $string);
$values = array();
foreach($pairs as $pair) {
list($key, $value) = explode('=', $pair);
$values[$key] = $value;
}
return $values;
}

cakephp how to group file data when upload multiple file

I use cakephp FormHelper to generate html form code.
echo $this->Form->create('newGallery.', array('type'=>'file'));
echo $this->Form->input('Photos',array(
'type' => 'file',
'label' => 'Photos (jpg,png,gif)',
'name' => 'upload[]',
'required',
'multiple'
));
echo $this->Form->end('Create');
but result is
Array
(
[upload] => Array
(
[name] => Array
(
[0] => Pic1.jpg
[1] => Pic2.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\phpB073.tmp
[1] => C:\wamp\tmp\phpB084.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 216302
[1] => 107102
)
)
)
I need to group the data of the same file so that i can deal with by using foreach loop. This is the result i need
Array
(
[upload] => Array
(
[0] => Array
(
[name] => Pic1.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpB073.tmp
[error] => 0
[size] => 216302
)
[1] => Array
(
[name] => Pic2.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpB084.tmp
[error] => 0
[size] => 107102
)
)
)
Thank you indeed for your help.
I think this can help you :)
/* create new empty array */
$result=array();
/* create new index */
$i=0;
/* foreach in your $upload or other array */
foreach($upload['upload'] as $key => $val){
foreach($val as $sub_val){
$result[$i][$key]=$sub_val;
$i++;
}
$i=0;
}