How to form an object from a php array - php

I am trying to form my data to a specific formate. I have normal data coming from a query like this
[
{
category: "Business"
},
{
category: "Events"
},
{
category: "Vip Meetup"
}
]
I want to form above data using a foreach loop to bellow structure
[
'Business' => __( 'Business', 'plugin-domain' ),
'Events' => __( 'Events', 'plugin-domain' ),
'Vip Meetup' => __( 'Vip Meetup', 'plugin-domain' ),
];
How can I create a data structure like this?
Thank you in advance...

I suppose this should be somehting like:
$options = [];
foreach ($eventCats as $cat) {
// Add an element to array under key `$cat->category`
// Value of the element will be result of calling `__` function
$options[$cat->category] = __( $cat->category, 'plugin-domain');
}

Related

Is there a way to retrieve data from a php array?

I'm trying to get managers to fill out a form using a plugin in WordPress, but the data keeps returning "-" instead of the actual data from an array.
I've tried modifying the pages that relate to the performance reviews as well as looked at the functions.php file.
This is the code that I assume keeps giving me the "-".
<?php echo isset( $performance_trainingstage[ $row-> trainingstage ] ) ? $performance_trainingstage[ $row-> trainingstage ] : '-'; ?>
This is the code that I assume the above code is supposed to be pulling from:
<?php erp_html_form_input( array(
'label' => __( 'Training Stage', 'erp' ),
'name' => 'trainingstage',
'value' => '',
'class' => 'erp-hrm-select2',
'type' => 'select',
'id' => 'performance_trainingstage',
'options' => array("FOH 1", "FOH 2", "FOH 3", "BOH 1", "BOH 2", "BOH 3", "Prep", "Champion of Excellence", "Team Trainer", 'erp') + erp_performance_rating()
) ); ?>
I would like the results to give me whatever option it is I selected: FOH 1, FOH 2, etc.

Create custom field inside OptionsetField in Silverstripe CMS 3.1

I want to choose the type of my calendar events from some predefined values but also, create a new (custom) type if it's not listed.
So i have created the field in $db like so:
'Type' => 'Varchar',
'EventCustomType' => 'Varchar'
Then, in the getCMSFields() i have:
$f->addFieldsToTab("Root.Main", $eventType = new OptionsetField(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => TextField::create('EventCustomType','Event type')
)
)
);
The problem is that i don't know how to insert the label "Custom" before the Textareafield and style them in the same line.
Also, i'm not sure if i need a second field for the custom one. Can i insert the custom value inside "Type" field or validate it ?
Thanks for any suggestions
This could be achieved by having a separate field for "EventCustomType" and then using Display Logic to show it with something like...
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$fEventCustomType = TextField::create('EventCustomType','Event type')
->displayIf('Type')->isEqualTo('custom');
$f->addFieldsToTab("Root.Main", array($eventType,$fEventCustomType));
As an alternative if you wanted to rescue This module then you could create this to save into one field as it is designed to do as you are asking... but it is with an error (last time I tried) so it is refernce only for now.
Finally, i have figured it out with separate fields:
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$customEventType = TextField::create('EventCustomType','Custom type');
$f->addFieldsToTab("Root.Main", array($eventType,$customEventType));
and jQuery:
$('#Root_Main').entwine({
onmatch: function(){
var $tab = this.closest('.tab');
var $eventType = $tab.find('ul.eventType');
var $customType = $tab.find('.customEventType').hide();
if($eventType.find('input:checked').val() == 'custom'){
$customType.show();
}
$eventType.find('input').change(function(){
if($(this).val() == 'custom'){
$customType.show();
}else{
$customType.hide();
}
});
}
});

Cannot autocomplete other textfields using CJuiAutoComplete

I have a model that has fields id_peg, nama_peg, and jabatan_peg. So here's what I wanna do:
Autosuggestion that shows the list ofid_peg as I type in the text field id_peg
When I select the suggested id_peg, the fields nama_peg and jabatan_peg are autocompleted based on that corresponding selected value (retrieved from database)
I've been trying to do the first one and I did it. But I'm stuck at the second one.
This is what I did:
view/melaporkan.php:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test1',
'source' => $this->createUrl('search'),
// additional javascript options for the autocomplete plugin
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui) {
$('#nama_peg').val(ui.item.nama_peg);
$('#jabatan_peg').val(ui.item.jabatan_peg);
}',
),
));
controllers/sitecontroller.php
public function actionSearch() {
$res = array(0 => array('id' => 1, 'value' => "id_peg"), 1 => array('id' => 2, 'value' => "jabatan_peg"), 2 => array('id' => 2, 'value' => "nama_peg"));
if (isset($_GET['term'])) {
$qtxt = "SELECT id_peg FROM pegawai WHERE id_peg LIKE :id_peg";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":id_peg", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
$res = $command->queryColumn();
}
echo CJSON::encode($res);
Yii::app()->end();
}
All it did was autosuggest for id_peg. When clicked some random id_peg, nama_peg and jabatan_peg were still empty.
What did I do wrong?
Actually the problem is controllers/sitecontroller.php page.
You are sending only id_peg as JSON format but trying to get as ui.item.jabatan_peg.
It is always better to send your value as
[{"id":"Caprimulgus europaeus","value":"European Nightjar"}] pattern
and get the value ui.item.value at your select function.

how to create treeview in yii framework

I want to create a tree view which fetching
data from Database and after that
order it by parent field
So table fields are included :
product_category_id
product_category_parent_id
product_category_name
the record that is been mentioned
by name " product_category_parent_id"
is 0 without any root and when it wish have any
ID code / number , parent Id should come in this palce
so structure of the table must be sent to the
View :
<ul><li><ul><li></li></ul></li></ul>
There is an example how to create CTreeView
private function generateTree($models)
{
$data = array();
foreach ($models as $category) {
$data[$category->id] = array(
'id' => $category->id,
'text' => ''.$category->category_name.'',
);
foreach ($category->goods as $item) {
$data[$category->id]['children'][$item->id] = array(
'id' => $item->id,
'text' => ''.$item->article.'',
'expanded' => false,
);
}
}
return $data;
}
In view
$this->widget('CTreeView', array('data' => $data,'persist'=>'cookie'));

Add a multidimension PHP Array at the end of each item of a multidimension array

Here are the two Multi-dimension arrays explained.
// Categories Array
$categories = array(
array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data'),
array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data'),
array('cat_id'=>'3', 'cat_name' => 'Category Tree', 'cat_data' => 'Some Data')
);
// Products Array (One $products array is to be placed inside every new category.
$products = array(
array('p_id'=>'1', 'p_name'=>'Product One'),
array('p_id'=>'2', 'p_name'=>'Product Two'),
array('p_id'=>'3', 'p_name'=>'Product Three')
);
Here The $products needs to be placed inside every element of $category array, with a key of some random key take for eg 'product_list'.
Here is the result like
$category = array(
array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data', 'product_list'=>array()),
array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data', 'product_list'=>array())
);
Please scroll Right for the above code to see the last element added to these elements.
Please tell how to add that multi-dimension array with a key to each n every element of the $category array. Thanks
What is the problem?
foreach ($categories as &$category) {
$category['product_list'] = $products;
}
Try to use this code.
foreach($categories as $key=>$value)
{
$categories[$key]['product_list'] = $products;
}
After trying several attemps, I solved this problem by myself by just a simple code. here it is.
$categories['product_list'] = $products;
Hope, users finding this type of problem this useful. Thanks

Categories