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';
}
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);
}
}
I am working on CakePHP. Recently I started using the CakePHP Hash Class. I want to convert an array format using the Hash Class functions.
I have the following array :
$arr = array(
0 => array(
'key1' => array(
'id' => 6
'type' => insert
'field_id' => 2
'activity' => table
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 6
)
1 => array(
'id' =>
'key1_id' => 6
)
)
)
1 => array(
'key1' => array(
'id' => 5
'type' => edit
'field_id' => 3
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 5
)
1 => array(
'id' =>
'key1_id' => 5
)
)
)
2 => array(
'key1' => array(
'id' => 4
'type' => insert
'field_id' => 2
'activity' => table
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 4
)
)
)
3 => array(
'key1' => array(
'id' => 3
'type' => insert
'field_id' => 3
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 3
)
)
)
4 => array(
'key1' => array(
'id' => 2
'type' => edit
'field_id' => 1
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 2
)
)
)
5 => array(
'key1' => array(
'id' => 1
'type' => edit
'field_id' => 3
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 1
)
)
)
);
The condition is that any key1 having type, field_id and activity exactly will result in all the key2 being merged together and the key1 that occurs later in the list gets unset.
I want to convert it into the following format :
$arr = array(
0 => array(
'key1' => array(
'id' => 6
'type' => insert
'field_id' => 2
'activity' => table
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 6
)
1 => array(
'id' =>
'key1_id' => 6
)
2 => array(
'id' =>
'key1_id' => 4
)
)
)
1 => array(
'key1' => array(
'id' => 5
'type' => edit
'field_id' => 3
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 5
)
1 => array(
'id' =>
'key1_id' => 5
)
2 => array(
'id' =>
'key1_id' => 1
)
)
)
3 => array(
'key1' => array(
'id' => 3
'type' => insert
'field_id' => 3
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 3
)
)
)
4 => array(
'key1' => array(
'id' => 2
'type' => edit
'field_id' => 1
'activity' => list
),
'key2' => array(
0 => array(
'id' =>
'key1_id' => 2
)
)
)
);
If you have a look key2 value for key '2' is merged with key '0' and '2' is unset.
The missing key values should basically be unset. I want to know the best possible way in which I can attain this format for the array.
What you are trying to do it's bit tricky. I recommend to achieve this by trying with some different CakePHP sql query.
Solution
$keys = Hash::extract($arr, '{n}.key1.field_id');
$vals = Hash::extract($arr, '{n}');
$result = $this->array_combine_($keys, $vals);
function array_merge_multiple(&$v) {
$z1 = array();
$z2 = array();
$len = count($v);
for ($i = 0; $i < $len; $i++) {
if ($i < ($len - 1)) {
$z1 = array_merge($v[$i]['key2'], $v[$i + 1]['key2']);
$z2 = array_merge($v[$i]['key1'], $v[$i + 1]['key1']);
} else if($len==1){
$z1 = $v[$i]['key2'];
$z2 = $v[$i]['key1'];
}
unset($v[$i]);
}
$v['key1'] = $z1;
$v['key2'] = $z2;
}
/* Modified array_combine function ( Hash::combine uses array_combine internally */
function array_combine_($keys, $values) {
$result = array();
foreach ($keys as $i => $k) {
$result[$k][] = $values[$i];
}
array_walk($result, 'array_merge_multiple');
return $result;
}
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')
));
I am using cakephp 2.1 and I wanna get average of movie ratings for perticular movie.
Where 'Movie' is a model and it contains 'Language' and 'CriticReview' as models.
The array structure is as follows.
array(
(int) 0 => array(
'Movie' => array(
'id' => '4',
'name' => 'Maattrraan',
'cover_image' => '/movies/4d0d1c41d956a2cb2ab93603d1fdf70c.jpg',
'release' => '2012-10-12',
'runtime' => '',
'budget' => '65 crore',
'box_office' => '',
'language_id' => '2',
'industry_id' => '3'
),
'Language' => array(
'id' => '2',
'name' => 'tamil'
),
'CriticReview' => array(
(int) 0 => array(
'rating' => '4',
'movie_id' => '4'
),
(int) 1 => array(
'rating' => '3',
'movie_id' => '4'
)
)
),
(int) 1 => array(
'Movie' => array(
'id' => '1',
'name' => 'Romeo',
'cover_image' => '/movies/32aa2788fa5e1584d4c627c56214574e.jpg',
'release' => '2012-07-06',
'runtime' => '',
'budget' => '',
'box_office' => '',
'language_id' => '1',
'industry_id' => '1'
),
'Language' => array(
'id' => '1',
'name' => 'kannada'
),
'CriticReview' => array(
(int) 0 => array(
'rating' => '6',
'movie_id' => '1'
),
(int) 1 => array(
'rating' => '3',
'movie_id' => '1'
)
)
));
So I have to avg of critic review. Please help me to find out the solution. The work will be appreciable.
I don't know how your query looks like now, but you can try to add this field:
$this->Movie->find("all", array(
"fields" => array("AVG(CriticReview.rating) AS AverageRating"),
"conditions" => ...
));
The array returned will now contain a sub array within each movie with a key 0 like this
[0] => Array
(
[AverageRating] => AVG_CALCULATED_BY_MYSQL
)
I personally prefer to save calculated data instead of calculate it on fly. Maybe you'd like to read this question: MySQL - Calculating fields on the fly vs storing calculated data