How to show dynamic output in the wp_bakery vc_map dropdown? - arrays

I am using the wp_bakery page builder plugin. What I am doing is, I have to display the custom output in the dropdown
Expected output
Current Output
if I use the below code then I am getting the correct output.
$params=array(
'name' => __('Speaker'),
'base' => 'speaker',
'category' => __(My shortcodes'),
'class' => 'js-example-basic-single',
"icon" => get_template_directory_uri() . "/images/shortcode_slider.png",
'params' => array(
array(
'type' => 'dropdown',
'heading' => __('Speakers'),
'param_name' => 'speakersname',
'class' => 'js-example-basic-single',
'admin_label' => true,
//'value' => array($selectSpeaker),
'value' => array(__( 'Item One', 'textdomain' ) => '1'), //static added
'description' => __('Select speakers')
))
);
echo"<pre />";
print_r($params);
vc_map( $params );
array output
Array
(
[name] => Speaker
[base] => speaker
[category] => My shortcodes
[class] => js-example-basic-single
[icon] => https://example.com/wordpress/wp-content/themes/testing/images/shortcode_slider.png
[params] => Array
(
[0] => Array
(
[type] => dropdown
[heading] => Speakers
[param_name] => speakersname
[class] => js-example-basic-single
[admin_label] => 1
[value] => Array
(
[Item One] => 1
)
[description] => Select speakers
)
)
)
But If I use the below code then I am getting the current output(Screenshot shared above)
$speakerpost_args = array( 'post_type'=>'speaker','post_status' => 'publish', 'posts_per_page' => 80);
$speaker_posts = wp_get_recent_posts($speakerpost_args);
$selectSpeaker="";
foreach ($speaker_posts as $key => $value) {
$selectSpeaker.="__( 'Item One', 'textdomain' ) => '1',";
}
$params=array(
'name' => __('Webinar Speaker'),
'base' => 'speaker',
'category' => __('Hybreed shortcodes'),
'class' => 'js-example-basic-single',
"icon" => get_template_directory_uri() . "/images/shortcode_slider.png",
'params' => array(
array(
'type' => 'dropdown',
'heading' => __('Speakers'),
'param_name' => 'speakersname',
'class' => 'js-example-basic-single',
'admin_label' => true,
'value' => array($selectSpeaker), //dynamic value
//'value' => array(__( 'Item One', 'textdomain' ) => '1'),
'description' => __('Select speakers')
))
);
echo"<pre />";
print_r($params);
vc_map( $params );
Array output
Array
(
[name] => Speaker
[base] => speaker
[category] =>My shortcodes
[class] => js-example-basic-single
[icon] => https://exmple.com/wordpress/wp-content/themes/testing/images/shortcode_slider.png
[params] => Array
(
[0] => Array
(
[type] => dropdown
[heading] => Speakers
[param_name] => speakersname
[class] => js-example-basic-single
[admin_label] => 1
[value] => Array
(
[0] => __( 'Item One', 'textdomain' ) => '1' // here is the issue
)
[description] => Select speakers
)
)
)

Need to append values as array items not as string concatenation.
Change
$selectSpeaker="";
foreach ($speaker_posts as $key => $value) {
$selectSpeaker.="__( 'Item One', 'textdomain' ) => '1',";
}
To:
$selectSpeaker=[];
foreach ($speaker_posts as $key => $value) {
$selectSpeaker[__( 'Item One', 'textdomain' )] = '1';
}

Related

change json data to simple php array

i want to change some JSON object data to PHP array
Array i have :-
'line_items' =>
array (
0 => '{"title":"ADIDAS | KID\'S STAN SMITH - 1 / white","product_id":1395863355492,"price":90,"variant_id":12486979027044,"quantity":2,"applied_discount":{"title":"DISCOUNT"}}',
1 => '{"title":"ADIDAS | CLASSIC BACKPACK - OS / black","product_id":1395863945316,"price":70,"variant_id":12486982500452,"quantity":5,"applied_discount":{"title":"DISCOUNT","value_type":"fixed_amount","value":"10.00","amount":"50.00"}}',
2 => '{"title":"Activity Auto - small / white","product_id":1396022444132,"price":14.99,"variant_id":12488662089828,"quantity":4,"applied_discount":{"title":"DISCOUNT"}}',
),
Expected Array:
'line_items' =>
array (
0 =>
array (
'title' => 'ADIDAS | KID\'S STAN SMITH - 1 / white',
'product_id' => '1395863355492',
'price' => '90',
'variant_id' => '12486979027044',
'quantity' => '2',
'applied_discount' =>
array (
'title' => 'DISCOUNT',
),
),
1 =>
array (
'title' => 'ADIDAS | CLASSIC BACKPACK - OS / black',
'product_id' => '1395863945316',
'price' => '70',
'variant_id' => '12486982500452',
'quantity' => '5',
'applied_discount' =>
array (
'title' => 'DISCOUNT',
'value_type' => 'fixed_amount',
'value' => '10.00',
'amount' => '50.00',
),
),
2 =>
array (
'title' => 'Activity Auto - small / white',
'product_id' => '1396022444132',
'price' => '14.99',
'variant_id' => '12488662089828',
'quantity' => '4',
'applied_discount' =>
array (
'title' => 'DISCOUNT',
),
),
),
Try this:
for ($i = 0; $i < count($arr_you_gave['line_items']); ++$i) {
$arr_you_gave['line_items'][$i] = json_decode($arr_you_gave['line_items'][$i], true);
}
Or you don't want to use any loop...
$corp_array = '[' . implode(',', $arr_you_gave['line_items']) . ']';
$arr_you_gave['line_items'] = json_decode($corp_array, true);

What is wrong with this association ? Related data are not being fetched

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

CakePHP 2.x retriving data with i18n in a loop

I'm looping trough Features (belongTo FeatureType) and find() FeatureType.name for each Feature.feature_type_id.
I'm getting first record correct (with i18n translation) but in all the rest the i18n translation is not in place, but given as separate record.
What I am doing wrong?
this is the debug of my result table:
array(
'FeatureType' => array(
'id' => '28',
'name' => 'kolor suwaka',
'comment' => 'kolor suwaka etui notebook',
'locale' => 'pol'
)
)
array(
'FeatureType' => array(
'id' => '7',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor materiału',
'FeatureType__i18n_comment' => 'gra w klasy'
)
)
array(
'FeatureType' => array(
'id' => '11',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor',
'FeatureType__i18n_comment' => 'kółko i krzyżyk (jasnoszare i ciemnoszare)'
)
)
array(
'FeatureType' => array(
'id' => '27',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'obwód głowy',
'FeatureType__i18n_comment' => 'rozmiar kapelusza czarownicy'
)
)
This is the code:
$features_str = "";
if (!empty($product['Features'])) {
foreach ($product['Features'] as $featureIndex => $feature) {
$featureTypeModel = new FeatureType();
$feature_type = $featureTypeModel->find("first", array("conditions" => array("FeatureType.id" => $feature['feature_type_id'])));
if (strlen($features_str) > 0) $features_str .= ", ";
$features_str .= $feature_type['FeatureType']['name'] . ': ' . $feature['name'];
unset($featureTypeModel);
}
}

CakePHP contain key issue

Hi i wrote contain on my model it effects only on first record and the rest of them
contains all the unnecessary records.
My Controller logic :
$findFiles['conditions']['Content.created_by'] = $curUser;
$findFiles['conditions']['KmpContent.is_approved'] = false;
$findFiles['conditions']['KmpContent.is_file'] = true;
$findFiles['contain'] = array('Parent' => array('SectionApprover' => array('Approver')));
$findFiles['recursive'] = 3;
$findFiles['fields'] = array('id', 'file_path', 'url_path', 'is_approved', 'name', 'created', 'approved_by','parent_id','display_text','is_active');
$fileData = $this->KmpContent->find('all', $findFiles);
The array Details are as follows
0 =>
array (
'KmpContent' =>
array (
'id' => '881',
'file_path' => '/uploads/test_file_for_multiple_approvers_backend_validations.docx',
'url_path' => '/uploads/test_file_for_multiple_approvers_backend_validations.docx',
'is_approved' => '0',
'name' => 'file_test_file_for_multiple_approvers_backend_validations.docx',
'created' => '2013-08-20 11:49:58',
'approved_by' => NULL,
'parent_id' => '143',
'display_text' => 'test file for multiple approvers',
'is_active' => '1',
),
'Parent' =>
array (
'id' => '143',
'name' => 'sct_Services.html',
'display_text' => 'Services',
'Parent' =>
array (
),
'Uploader' =>
array (
),
'SectionApprover' =>
array (
0 =>
array (
'id' => '104',
'kmp_content_id' => '143',
'approver_id' => '882',
'Approver' =>
array (
'id' => '882',
'first_name' => 'Prakasha',
'last_name' => 'Matte',
),
),
1 =>
array (
'id' => '256',
'kmp_content_id' => '143',
'approver_id' => '1168',
'Approver' =>
array (
'id' => '1168',
'first_name' => 'Rima Roy',
'last_name' => 'Chowdhury',
),
),
2 =>
array (
'id' => '257',
'kmp_content_id' => '143',
'approver_id' => '1171',
'Approver' =>
array (
'id' => '1171',
'first_name' => 'Durga Venkatesh',
'last_name' => 'Sambhani',
),
),
3 =>
array (
'id' => '258',
'kmp_content_id' => '143',
'approver_id' => '1172',
'Approver' =>
array (
'id' => '1172',
'first_name' => 'Gowtham Babu',
'last_name' => 'Tummala',
),
),
4 =>
array (
'id' => '259',
'kmp_content_id' => '143',
'approver_id' => '889',
'Approver' =>
array (
'id' => '889',
'first_name' => 'Sandeep',
'last_name' => 'Pathuri',
),
),
),
),
'SectionApprover' =>
array (
),
),
1 =>
array (
'KmpContent' =>
array (
'id' => '882',
'file_path' => '/uploads/test_file_for_ma_contentcontroller_save.txt',
'url_path' => '/uploads/test_file_for_ma_contentcontroller_save.txt',
'is_approved' => '0',
'name' => 'file_test_file_for_ma_contentcontroller_save.txt',
'created' => '2013-08-20 11:54:15',
'approved_by' => NULL,
'parent_id' => '28',
'display_text' => 'test file for MA',
'is_active' => '1',
),
'Parent' =>
array (
'id' => '28',
'name' => 'sct_domain.html',
'display_text' => 'Domains',
'Parent' =>
array (
),
'Uploader' =>
array (
),
'SectionApprover' =>
array (
0 =>
array (
'id' => '26',
'kmp_content_id' => '28',
'approver_id' => '467',
'KmpContent' =>
array (
'id' => '28',
'name' => 'sct_domain.html',
'description' => NULL,
'page_title' => 'IVV Knowledge Portal :: mckesson :: domain',
'display_logo_path' => '../images/domains.png',
'display_text' => 'Domains',
'top_logo_path' => '/mckesson_ban.jpg',
'top_logo_url' => 'http://www.mckesson.com/en_us/McKesson.com/',
'header_text' => 'Domains',
'header_desc' => NULL,
'header_logo_path' => '',
'is_section' => '1',
'url_path' => '/university/mckesson/domains.html',
'can_upload_files' => '1',
'is_file' => '0',
'file_path' => NULL,
'display_order' => '1',
'aco_path' => '',
'menu_name' => 'mckesson',
'view_path' => 'download_files',
'is_approved' => '1',
'parent_id' => '11',
'is_active' => '1',
'created' => '2013-06-22 21:18:27',
'created_by' => NULL,
'modified' => '2013-06-22 21:21:01',
'approved_by' => NULL,
),
'Approver' =>
array (
'id' => '467',
'first_name' => 'Nageswara Rao',
'last_name' => 'Menda',
),
),
),
),
'SectionApprover' =>
array (
),
),
I dont want the kmpContent in with in the sectionapprovers for all the records
What if you formatted the statement like
$fileData = $this->KmpContent->find('all', array(
'conditions' => array(
'KmpContent.created_by' => $curUser,
'KmpContent.is_approved' => false,
'KmpContent.is_file' => true,
),
'contain' => array(
'Parent' => array(
'SectionApprover' => array(
'Approver'
)
)
),
'recursive' => 3, // though you don't need to set recursive with contain
'fields' => array('id', 'file_path', 'url_path', 'is_approved', 'name', 'created', 'approved_by','parent_id','display_text','is_active')
));

How does Set::nest in Cakephp 2 works?

Due to the lack of proper documentation I have difficulties
to understand "Set::nest" CakePhp function.
I would appreciate if somebody can explain generally
and with my example how to use it properly.
Let's say I have this tree:
array(
(0) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 1'
),
'Day' => array('date' => '2012-01-01')
),
(1) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 1'
),
'Day' => array('date' => '2012-01-02')
),
(2) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array('date' => '2012-01-06')
),
(3) => array(
'Category' => array('name' => 'Cat 2'),
'Course' => array(
'name' => 'Course 2'
),
'Day' => array('date' => '2012-01-02')
),
)
to be transformed to:
array(
'Cat 1' => array(
'Course' => array(
'name' => 'Course 1'
),
'Day' => array(
array('date' => '2012-01-01'),
array('date' => '2012-01-02')
),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array(
array('date' => '2012-01-06')
),
),
'Cat 2' => array(
'Courses' => array(
'name' => 'Course 2'
),
'Day' => array(
array('date' => '2012-01-03')
)
),
)
As you can see it's kind of a double nesting because "Category" can have multiple "Course" rows and "Course" can have multiple "Day".
I could just loop through it and do it manually.
So the fist question is: can I transform this with Set::nest at all
and if yes how?
What you are asking for is impossible. Your array is not valid. You need to change your format to have each category as a nested array 'cat' => array(array('course 1' => ...), array('course 2' => ...))
print_r(array(
'Cat 1' => array(
'Course' => array(
'name' => 'Course 1'
),
'Day' => array(
array('date' => '2012-01-01'),
array('date' => '2012-01-02')
),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array(
array('date' => '2012-01-06')
),
),
'Cat 2' => array(
'Courses' => array(
'name' => 'Course 2'
),
'Day' => array(
array('date' => '2012-01-03')
)
),
));
Output:
Array
(
[Cat 1] => Array
(
[Course] => Array
(
[name] => Course 3
)
[Day] => Array
(
[0] => Array
(
[date] => 2012-01-06
)
)
)
[Cat 2] => Array
(
[Courses] => Array
(
[name] => Course 2
)
[Day] => Array
(
[0] => Array
(
[date] => 2012-01-03
)
)
)
)

Resources